我对uialertcontroller有疑问。 uialertview完美无缺,但事实并非如此。我有这个:
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];
消息显示两秒钟并立即执行处理程序。我想按下按钮确定时按下它但不起作用。我做错了什么?
答案 0 :(得分:2)
删除dismiss块,然后单击OK按钮调用处理程序。
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];
答案 1 :(得分:1)
请尝试使用此代码
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];
答案 2 :(得分:0)
您不必从完成区内取消警报控制器,因为它会自动解除。
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];
对于使用addAction:
方法添加的每个操作,警报控制器会配置一个包含操作详细信息的按钮。当用户点击该操作时,警报控制器会执行您在创建操作对象时提供的块。