0

当我按下“确定”按钮时,UIAlertView我希望它返回, UITableViewController但当我单击它时,它不会返回。

QuizViewController.h

@interface QuizViewController : UIViewController <UIAlertViewDelegate> { 
}

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

@end

QuizViewController.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]; 
}
4

4 回答 4

0

使用此代码返回。

[self.navigationController popViewControllerAnimated:YES];
于 2012-07-30T08:43:50.143 回答
0

您不应该在 .h 文件中声明委托方法,例如: -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
而是在 alertView 中提供委托: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>

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

于 2012-07-30T07:37:28.373 回答
0

用这个:

    - (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-30T07:39:34.893 回答
0

您没有被回调的原因是您将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]; 
于 2012-07-30T07:44:52.073 回答