2012-05-02 59 views
1

UIAlertView關閉後,我想重新加載視圖(對視圖運行viewWillDisappear,對視圖運行viewWillAppear)。如何在UIAlertView關閉後重新載入視圖?

我該怎麼做?我曾嘗試委託給無和自我。這是我的代碼。

- (IBAction)doStuffWhenUIButtonIsClicked:(id)sender 
{ 
    int intSelection = [sender tag]; 
    NSString* strAlertTitle; 
    NSString* strAlertMessage; 
    if (intSelection == self.intCorrectChoice) { 
     strAlertTitle = @"Correct tag!"; 
    } 
    else { 
     strAlertTitle = @"Sorry, incorrect tag!"; 
    } 
strAlertMessage = @"The correct answer was 8."; 

    self.answerReaction = [[UIAlertView alloc] 
          initWithTitle: strAlertTitle 
           message: strAlertMessage 
           delegate: nil 
         cancelButtonTitle:@"Next Question" 
         otherButtonTitles:nil]; 
    [self.answerReaction show]; 
} 

如您所見,我想在用戶回答其中一個問題後加載一個新問題。

回答

5

首先,您需要使視圖控制器符合UIAlertViewDelegate協議。

然後,顯示警報之前,您需要添加此行:

self.answerReaction.delegate = self; 

然後,實施didDismissWithButtonIndex委託方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    [self viewWillDisappear:YES]; 
    // load your new question 
    [self viewWillAppear:YES]; 
} 

這應該照顧它!

0

只需添加這個方法,當你點擊UIAlertView上的任何按鈕時觸發。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     [self viewWillDisappear:YES]; 
     [self viewWillAppear:YES]; 
    } 
} 

希望它可以幫助...

0

它不會對我的工作!我有:

- (IBAction)OkButton:(id)sender {  

    // store your selected row 
    NSUInteger selectedRow = [_intervalPicker selectedRowInComponent:0];  

    // Display a message to user 
    if (selectedRow == CorrectAnswer) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct!" message:@"YAY!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 

     [alert show]; 

     RightScoreNumber0 += 1; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"Wrong answer" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 

     [alert show]; 

     WrongScoreNumber0 += 1; 
    } 

} 

,並如你所說:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    NSLog(@"OK was selected"); 

    [self viewWillDisappear:YES]; 
    // load your new question 
    [self viewWillAppear:YES]; 
} 

我得到的消息「入選OK」,但是沒有什麼事情發生......

這最後的工作,但在另一辦法!! 我更換兩個

[self viewWillDisappear:YES]; 
[self viewWillAppear:YES]; 

[self viewDidLoad]; 
相關問題