Reputation: 1107
I have problem with uialertcontroller. uialertview works perfectly but this just won't. I have this:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(@"Success")
message:LocalizedString(@"Example") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:LocalizedString(@"Ok")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
[self dismissViewControllerAnimated:YES completion:^
{
[self performSelector:@selector(presentLogInViewController) withObject:nil];
}];
}];
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
Message is shown for two seconds and executes handler immediatly. I want to handle when button Ok is pressed but it does not work. What am I doing wrong?
Upvotes: 1
Views: 3815
Reputation: 1206
Remove the dismiss block and then the handler will be called on click of OK button.
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(@"Success")
message:LocalizedString(@"Example")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:LocalizedString(@"Ok")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self performSelector:@selector(presentLogInViewController) withObject:nil];
}];
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
Upvotes: 2
Reputation: 3442
You don't have to dismiss the alert controller from inside the completion block because it will dismiss automatically.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// here you can add the completion for when the OK button is pressed
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
For each action you add using the addAction:
method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object.
Upvotes: 0
Reputation: 286
Please try with this code
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:cancelAction];
[self presentViewController: alertController animated:YES completion:nil];
Upvotes: 1