2012-07-30 29 views
0

當我在UIAlertView處按'確定'按鈕時,我希望它回到UITableViewController,但是當我點擊它時不會返回。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]; 
} 
+0

指派委託:自我.... – Rajneesh071 2012-07-30 08:31:56

+0

我已經改變了,但它仍然無法正常工作,它除了有任何錯誤? – jaspanther 2012-07-30 08:35:36

+0

嘗試[self.navigationController popViewControllerAnimated:YES]; – Rajneesh071 2012-07-30 08:42:48

回答

0

在喜歡你的.h文件中你不應該聲明一個委託方法: -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
而不是給委託:自我在alertView這樣的:

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的點擊。

+0

的'代表:self'部分是很重要的,但沒有其他三個建議都停止工作了警報處理程序:宣佈在回調頭是不必要的,但不痛,宣稱要實現'UIAlertViewDelegate'是不必要的,他只有一個按鈕,這樣'buttonIndex'將始終爲0 – Nate 2012-07-30 09:09:51

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的代表應該宣佈這種方式,只是實現委託方法。

+0

我只有一個按鈕,我仍然需要使用,如果其他方法? – jaspanther 2012-07-30 08:08:08

+0

一旦顯示它,即釋放警報對象。當你分配警報對象時,說「委託:自我」,而不是「無」。 – 2012-07-30 08:15:28

+0

我改變了,但它仍然無法正常工作。 – jaspanther 2012-07-30 08:31:44

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]; 
+0

我改變了它,但它仍然不起作用。我只有1個「確定」按鈕。 – jaspanther 2012-07-30 08:14:17

+0

@jaspanther,我不確定我是否理解你的評論。您只有1個「確定」按鈕,因爲這是您在上面的行中創建的全部內容。如果你想要更多的按鈕,你必須通過'otherButtonTitles'參數添加它們。如果由於某種原因,您正在迴應**其他**人對'alertView:clickedButtonAtIndex:'方法的評論... ** no **,如果您只有一個按鈕,則不需要使用if語句。請提供更多信息。什麼不起作用?你的'alertView:clickedButtonAtIndex:'還沒有被調用? – Nate 2012-07-30 08:55:23

+0

@otakuProgrammer,您是否想要在其他地方發表此評論?也許在這個問題下? – Nate 2012-07-30 13:11:09

0

使用此代碼回去。

[self.navigationController popViewControllerAnimated:YES]; 
相關問題