我試圖將數組中的警報操作傳遞給用於將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 –