我正在使用 Objective-C 编写一些UIAlertController代码。
我有更多按钮,但按钮会显示不同UIAlertController的 s 并处理不同的UIAlertActionhandler 。
所以我想创建一个UIAlertController,和UIAlertAction。
如下所示:
-(void) initAlert{
alertController = [UIAlertController alertControllerWithTitle:@"hint" message:@"count down alert" preferredStyle:UIAlertControllerStyleAlert];
doneAction = [UIAlertAction actionWithTitle:@"okey" style:UIAlertActionStyleDefault handler:
^(UIAlertAction *action) {
NSLog(@"show log");
}];
[alertController addAction:doneAction];
}
-(void) showAlert{
[self presentViewController:alertController animated:YES completion:nil];
}
然后我想用不同IBAction的按钮来调用showAlert方法,并设置不同的UIAlertController标题,UIAlertAction标题和处理不同的alertAction处理程序。
但是我遇到了一些问题。
我在不同的按钮中调用该方法,如下所示:
- (IBAction)btn1Action:(UIButton *)sender {
alertController.title = @"controller 1";
alertController.message = @"message1";
[self showAlert];
}
- (IBAction)btn2Action:(UIButton *)sender {
alertController.title = @"controller 2";
alertController.message = @"message2";
[self showAlert];
}
我不知道如何UIAlertAction用相同的 doneAction 更改标题,我搜索了一些数据显示UIAlertActionis readyonly 属性。
那么还有其他方法可以更改UIAlertAction标题吗?或者我们可以删除UIAlertController addAction:添加其他的方法UIAlertAction吗?
以及如何将不同UIAlertAction的处理程序传递给 AlertAction 以供UIAlertController使用?
非常感谢。