UIAlertView关闭后如何重新加载视图?

时间:2012-05-02 05:11:52

标签: objective-c ios

关闭UIAlertView后,我想重新加载一个视图(为视图运行viewWillDisappear,为视图运行viewWillAppear

我该怎么做?我试过委托nil和self。这是我的代码。

- (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];
}

如您所见,我想在用户回答其中一个问题后加载一个新问题。

3 个答案:

答案 0 :(得分:5)

首先,您需要使视图控制器符合UIAlertViewDelegate协议。

然后,在显示警报之前,您需要添加以下行:

self.answerReaction.delegate = self;

然后,实现didDismissWithButtonIndex委托方法:

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

应该照顾它!

答案 1 :(得分:0)

只需添加此方法,当您单击UIAlertView上的任何按钮时触发该方法。

- (void)alertView:(UIAlertView *)alertView  clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self viewWillDisappear:YES];
        [self viewWillAppear:YES];
    }
}
希望它有所帮助...

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

我收到消息“确定被选中”,但其他事情却没有发生......

它终于奏效了,但另一种方式!! 我替换了两个

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

[self viewDidLoad];