当我在UIAlertView上按下'OK‘按钮时,我想让它返回到UITableViewController,但当我点击它时,它不会返回。
QuizViewController.h
@interface QuizViewController : UIViewController <UIAlertViewDelegate> { 
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@endQuizViewController.m
-(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]; 
}发布于 2012-07-30 15:37:28
您不应该在.h文件中声明委托方法,如:-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
取而代之的是在alertView中使用delegate:self,如下所示:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Results" message:[[NSString alloc] initWithFormat:@"Your total score will be %i!", myScore]
                                                    delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];并在.h文件中声明如下
@interface QuizViewController : UIViewController<UIAlertViewDelegate>然后单击button.index=0在您的委托方法中使用[self.navigationController presentModalViewController:quizTable animated:YES];。
发布于 2012-07-30 15:39:35
使用以下命令:
    - (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的委托不应该以这种方式声明,只要实现委托方法即可。
发布于 2012-07-30 15:44:52
没有回调的原因是您将UIAlertView委托设置为nil。需要将其设置为self,以便该对象在警报解除时接收回调:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Results"
                                                message:[[NSString alloc] initWithFormat:@"Your total score will be %i!", myScore]  
                                               delegate:self
                                      cancelButtonTitle:@"OK" otherButtonTitles: nil]; https://stackoverflow.com/questions/11717111
复制相似问题