UIAlertView到UITableViewController

时间:2012-07-30 07:33:13

标签: iphone uitableview xcode4.3 uialertview

当我按下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 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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的委托,只需实现委托方法。

答案 2 :(得分: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]; 

答案 3 :(得分:0)

使用此代码返回。

[self.navigationController popViewControllerAnimated:YES];