我试图将数组中的警报操作传递给用于将UIAlertController配置简化为一行的函数。 我能够成功传递按钮标题,但不是警报操作。 这是我正在做的事情。将数组中的闭包/块作为参数传递iOS
+(void)showAlertWithTitle:(NSString*)title
message:(NSString*)alertmessage
buttonTitles:(NSArray*)buttonTitles
buttonActions:(NSArray*)buttonActions
inViewController:(UIViewController*)viewController {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:alertmessage preferredStyle:UIAlertControllerStyleAlert];
[buttonTitles enumerateObjectsUsingBlock:^(NSString* buttonTitle,NSUInteger idx,BOOL *stop){
UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler: [[buttonActions objectAtIndex:idx] copy]]; //blocks should always be copied to heap from stack else they will crash
[alert addAction:action];
}];
[viewController presentViewController:alert animated:YES completion:nil];
}
上面的代码文件写得很长,所以它在目标c中。 我写了一些新的文件,这些文件在swift中,我正在用swift调用上面的方法,如下所示。
CommonManager.showAlert(withTitle: "", message: "Feedback Sent",
buttonTitles: ["Ok"], buttonActions: [ { (action: UIAlertAction) in
print("you pressed Ok alert button");
// call method whatever u need
}], in: self)
如果我没有通过关闭工作正常,如果通过关闭时点击确定它崩溃。 我还发现,我们需要复制一个块,当它作为一个集合传递,我做到了,但有些东西仍然是不正确的,我无法弄清楚。你能告诉我我需要在这里做什么吗?
感谢
该解决方案为我工作,谢谢@newacct –