atomikpanda
atomikpanda

Reputation: 1886

cancel UIAlertView programmatically

How can I cancel a UIAlertView programmatically? This is the code that I use to show the UIAlertView

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Save Background" message:@"Downloading..." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 70.0);
    [myAlertView setTransform:myTransform];
    [myAlertView show];

Upvotes: 7

Views: 3695

Answers (2)

Justin Boo
Justin Boo

Reputation: 10198

Try to use dismissWithClickedButtonIndex: like this:

Here is the code to use:

[myAlertView dismissWithClickedButtonIndex:-1 animated:YES];

Upvotes: 11

Michael Dautermann
Michael Dautermann

Reputation: 89509

You could do it via

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

while calling the "cancel" button index.

You could also simply remove the alert view from the superview (i.e. "removeFromSuperview"). Make sure to "release" the alloc'd view in case you are not using ARC.

Upvotes: 4

Related Questions