gpt4 book ai didi

ios - UIAlertController作为接受功能参数的实用程序

转载 作者:行者123 更新时间:2023-12-01 18:07:35 25 4
gpt4 key购买 nike

我有这个UIAlertController作为接受两个参数(标题和内容)的实用程序。我想修改“确认”按钮。我想复制此实用程序并添加另一个参数,它将执行特定功能。

-(UIAlertController *) modalWithTitle : (NSString *) title andContent: (NSString *) content{

UIAlertController *alert = [UIAlertController alertControllerWithTitle: title message:content preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];

[alert addAction:defaultAction];
return alert;
}

示例代码:
UIAlertController *alert =[[ModalController alloc] modalWithTitle:@"Error" andContent:@"Network unavailable."
andAction:<ENTER FUNCTION TO EXECUTE HERE>];
[self presentViewController:alert animated:YES completion:nil];

最佳答案

您可以这样写:

+ (UIAlertController *)modalWithTitle:(NSString *)title andContent:(NSString *)content andHandler:(void (^)(UIAlertAction *))handler {
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:handler];
[alert addAction:defaultAction];
return alert;
}

用法:
void (^handler)(UIAlertAction *) = ^(UIAlertAction *action) {
// code to execute
};
[[ModalController alloc] modalWithTitle:@"title" andContent:@"content" andHandler:handler];

另一种方法:
+ (UIAlertController *)modalWithTitle:(NSString *)title andContent:(NSString *)content andHandler:(void (^)(void))handler {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
handler();
}];
[alert addAction:defaultAction];
return alert;
}

用法:
void (^block)(void) = ^{
// code to execute
};
[[ModalController alloc] modalWithTitle:@"title" andContent:@"content" andHandler:block];

关于ios - UIAlertController作为接受功能参数的实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38047436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com