cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: remove a subview with cancelButtonTitel of UIAlert

Can I remove a subview with cancelButtonTitle of a UIAlert? Because I write this:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Done!" 
                                                    message:[@"It's all ok!"]
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];

[self dismissModalViewControllerAnimated:YES];

but this code remove my subview before I push cancelButtonTitle, how can I do?

Upvotes: 1

Views: 745

Answers (2)

Nick Weaver
Nick Weaver

Reputation: 47241

Yes, implement the UIAlertViewDelegate method alertView:didDismissWithButtonIndex: and dismiss your view there.

Set the delegate to self first:

...
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Done!" 
                                                    message:[@"It's all ok!"]
                                                   delegate:self
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
...

Could look like this:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
    if (buttonIndex == [alertView cancelButtonIndex]) {
        [self dismissModalViewControllerAnimated:YES];
    }
}

Upvotes: 2

MarkPowell
MarkPowell

Reputation: 16540

Set the delegate of the UIAlertView to self and implement:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

In this method, dismiss the view.

Upvotes: 0

Related Questions