首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIAlertView到UITableViewController

UIAlertView到UITableViewController
EN

Stack Overflow用户
提问于 2012-07-30 15:33:13
回答 4查看 266关注 0票数 0

当我在UIAlertView上按下'OK‘按钮时,我想让它返回到UITableViewController,但当我点击它时,它不会返回。

QuizViewController.h

代码语言:javascript
运行
复制
@interface QuizViewController : UIViewController <UIAlertViewDelegate> { 
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

@end

QuizViewController.m

代码语言:javascript
运行
复制
-(IBAction)buttonWasClicked:(id)sender{

UIButton *resultebutton= (UIButton*)sender;

if (qCount < totalQuestions) {

    id prevQuestion =  [appDelegate.qs objectAtIndex:qCount-1];
    NSString * correctAns = [prevQuestion labelAns];

    if ([correctAns isEqualToString:resultebutton.titleLabel.text]) 
        myScore += 5;            

    NSLog(@"The button title is %@ ", correctAns);
    NSLog(@"The button title is %@ ", resultebutton.titleLabel.text);

    NSString *finishingStatement = [[NSString alloc] initWithFormat:@"Your score so far is %i!", myScore];
    theScore.text = finishingStatement;



    id nextQuestion = [appDelegate.qs objectAtIndex:qCount];

 quizLbl.text = [nextQuestion labelQn];
    headerLbl.text = [nextQuestion labelHeader];

 [qBtn setTitle:[nextQuestion labelBtn] forState:UIControlStateNormal];
 [qBtnB setTitle:[nextQuestion labelBtnB] forState:UIControlStateNormal];
 [qBtnC setTitle:[nextQuestion labelBtnC] forState:UIControlStateNormal];
 [qBtnD setTitle:[nextQuestion labelBtnD] forState:UIControlStateNormal];



 qCount++;

 }


 else {

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Results" message:[[NSString alloc] initWithFormat:@"Your total score will be %i!", myScore]
                                                    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

     [alert show];


 }

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    QuizTableViewController *quizTable = [self.storyboard instantiateViewControllerWithIdentifier:@"quizTable"];

    [self.navigationController presentModalViewController:quizTable animated:YES]; 
}
EN

回答 4

Stack Overflow用户

发布于 2012-07-30 15:37:28

您不应该在.h文件中声明委托方法,如:-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

取而代之的是在alertView中使用delegate:self,如下所示:

代码语言:javascript
运行
复制
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Results" message:[[NSString alloc] initWithFormat:@"Your total score will be %i!", myScore]
                                                    delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

并在.h文件中声明如下

代码语言:javascript
运行
复制
@interface QuizViewController : UIViewController<UIAlertViewDelegate>

然后单击button.index=0在您的委托方法中使用[self.navigationController presentModalViewController:quizTable animated:YES];

票数 0
EN

Stack Overflow用户

发布于 2012-07-30 15:39:35

使用以下命令:

代码语言:javascript
运行
复制
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
        if([alertView.title isEqualToString:@"the_title_of_your_alert"]){
           //above line is to identify your alert, if you have several ones
           if(buttonIndex == 0)
               //do this
           else if (buttonIndex == 1)
               //do that
           else if //bla bla bla, find the button, and if it is your "ok" button, 
                   //go to your TableViewController
        }
    }

并且alertView的委托不应该以这种方式声明,只要实现委托方法即可。

票数 0
EN

Stack Overflow用户

发布于 2012-07-30 15:44:52

没有回调的原因是您将UIAlertView委托设置为nil。需要将其设置为self,以便该对象在警报解除时接收回调:

代码语言:javascript
运行
复制
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Results"
                                                message:[[NSString alloc] initWithFormat:@"Your total score will be %i!", myScore]  
                                               delegate:self
                                      cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11717111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档