How do I reload a view after a UIAlertView has been closed?

After a UIAlertView is closed, I would like to reload a view (run viewWillDisappear for the view and viewWillAppear for the view).

How do I do that? I have tried delegating to both nil and self. Here is my code.

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

As you see, I want to load a new question after the user answers one of them.

Answers


First, you need to make the view controller that you are in conform to the UIAlertViewDelegate protocol.

Then, before you show the alert, you need to add this line:

self.answerReaction.delegate = self;

Then, implement the didDismissWithButtonIndex delegate method:

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

That should take care of it!


just add this method which is triggered when u click any button on UIAlertView.

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

hope it helps...


It doesn't work for me! I have:

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

}

and as you suggested:

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

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

I get the message "OK was selected", but nothings else happens...

It finally worked but in another way!! I replace both

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

with

[self viewDidLoad];

Need Your Help

MPMoviePlayerController audio/video out of sync

objective-c audio mpmovieplayercontroller sync

In my iPad App, I create an MPMoviePlayerController that plays an MP4 off of a website. In the MP4 there are people talking. In the App, I have noticed that the audio and video are out of sync by...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.