我有以下实现可以重用整个应用程序。我将以下代码存储在 Utility.m 类中。
CustomViewController.m
如何在以下
中捕获点击事件[self presentViewController:[Utility oneButtonDisplayAlert:@"Error" withMessage:@"Please try again later"] animated:YES completion:nil];
Utility.m
+ (UIAlertController *)oneButtonDisplayAlert : (NSString*)title withMessage : (NSString*) message
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
[alert addAction:yesButton];
return alert;
}
答案 0 :(得分:1)
将块参数添加到oneButtonDisplayAlert:withMessage:
方法中。在警报操作的处理程序中调用该块。
+ (UIAlertController *)oneButtonDisplayAlert:(NSString *)title withMessage:(NSString *)message andOKHandler:(void (^)(void))handler
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (handler) {
handler();
}
}];
[alert addAction:yesButton];
return alert;
}
然后将其称为:
UIAlertController *alert = [Utility oneButtonDisplayAlert:@"Error" withMessage:@"Please try again later" andOKHandler:^{
// whatever code you need when OK tapped
}];
[self presentViewController:alert animated:YES completion:nil];
注意:此答案中的代码可能有拼写错误。语法未经验证。