2017-12-28 6 views
0

次の実装をアプリ全体で再利用することができます。私はUtility.mクラスに次のコードを格納します。アラートボタンクリックイベントを検出

CustomViewController.m

どのように私はあなたのoneButtonDisplayAlert:withMessage:メソッドにブロックパラメータを追加します。

+ (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

Utility.m次

[self presentViewController:[Utility oneButtonDisplayAlert:@"Error" withMessage:@"Please try again later"] animated:YES completion:nil]; 

でイベントをクリックしてキャプチャすることができます。アラートアクションのハンドラ内のブロックを呼び出します。

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]; 

注:

+ (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; 
} 

そしてとしてそれを呼び出すコードをこの答えにタイプミスがあるかもしれません。構文が検証されません。

関連する問題