UIAlertControllerの設定を1行に単純化するために使用する関数に、配列内のアラートアクションを渡そうとしています。 ボタンのタイトルを正常に渡すことはできますが、アラートアクションは成功させることはできません。 ここに私がやっていることがあります。パラメータとして配列内のクロージャ/ブロックを渡す
+(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です。 私は迅速なファイルをいくつか書いていますが、私は上記の方法を以下のように迅速に呼び出しています。
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)
私がクロージャを渡してもうまくいかない場合、OKをクリックするとクロージャを渡すとクラッシュします。 私はまた、コレクションとして渡されたときにブロックをコピーする必要があることを発見しました。しかし、私はこれを行いましたが、まだ分かりません。私はここで何をする必要があるか教えてくれますか?
おかげ
このソリューションは、私のために働いた、ありがとう@newacct –