2017-04-08 3 views
1

私は私のプロジェクトでのコンセプトの下にこれをしたい:カスタムビューのボタンクリックイベントを処理する方法は?

私はちょうどUIViewControllerを使用して一つの小さなカスタムポップアップを作成し、1つのメッセージラベルと2つのボタンを含むこのカスタムポップアップ、1が「OK」で、別のものを「キャンセル」です。このカスタムポップアップコーディングしてappdelegateです。このポップアップを開くときに、ポップアップメソッドをビューコントローラからこのアラートを開く必要があるときに呼び出しました。

問題は、「カスタムアラート」ポップアップの「OK」ボタンで異なる機能を実行したいということです。ですから、個々のViewControllerからこの「OK」ボタンのクリックイベントを管理する方法を手伝ってください。

[1]先進

+0

[このを見てみましょう](https://github.com/Darktt/DTAlertView) –

+0

私は、利用可能なカスタムアラートボックスの多くがあるので、あなたがcocoacontrols.comに見てお勧めします。 https://www.cocoacontrols.com/search?q=alert –

+0

いくつかのコードを入力できますか?このアラートを作成したか、またはcutomボタンのメソッド全体を作成しますか? –

答えて

1

おかげでユーティリティクラスで以下のメソッドを入れて、あなたのビューからそれを呼び出す[1] [] [!ここに画像の説明を入力します]私の添付スクリーンショット をご確認ください

[Utility showAlertWithTitle:@"ABC" msg:@"msg" vc:self positiveHandler:^(UIAlertAction *action) { 
    // Do here when ok is pressed 
} negativeHandler:nil]; //pass nil when cancel is pressed 

ようなコントローラにObjC

+ (void)showAlertWithTitle:(NSString *)title msg:(NSString *)msg vc:(UIViewController *)vc positiveHandler:(void (^ __nullable)(UIAlertAction *action))positiveHandler negativeHandler:(void (^ __nullable)(UIAlertAction *action))negativeHandler { 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *positiveAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:positiveHandler]; 
    [alertController addAction:positiveAction]; 

    UIAlertAction *negativeAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:negativeHandler]; 
    [alertController addAction:negativeAction]; 

    //show alert 
    [vc presentViewController:alertController animated:YES completion:nil]; 
} 

スウィフト

// Shows alert with yes no button 
static func showAlert(title: String, msg: String, vc: UIViewController, positiveActionHandler: ((UIAlertAction) -> Swift.Void)?, negativeActionHandler: ((UIAlertAction) -> Swift.Void)?) { 
let alertController = UIAlertController(title: title, message: msg, preferredStyle: .alert) 
let positiveAction = UIAlertAction(title: "Ok", style: .destructive, handler: positiveActionHandler) 
alertController.addAction(positiveAction) 

let negativeAction = UIAlertAction(title: "Cancel", style: .cancel, handler: negativeActionHandler) 
alertController.addAction(negativeAction) 
vc.present(alertController, animated: true, completion: nil) 
} 
+0

ありがとうございます!!!!! –

関連する問題