2017-04-08 113 views
1

我想這樣做,下面的概念在我的項目:如何處理自定義視圖的按鈕點擊事件?

我只是用UIViewController創建一個小的自定義彈出,這種含定製彈出一個消息標籤和兩個按鈕,一個是「OK」,另一種是「取消」。這定製popup編碼在appdelegate。現在,當我想要打開此彈出窗口時,我只需從視圖控制器中調用此應用程序門彈出方法,當我需要打開此警報時。

現在的問題是,我想在「自定義提醒」彈出窗口的「確定」按鈕上執行不同的功能。所以請幫助我如何管理個人ViewController中的「確定」按鈕單擊事件。

請檢查我的附截圖 [![在這裏輸入的形象描述] [1] [1]

在先進的感謝

+0

[看看這個(https://github.com/Darktt/DTAlertView) –

+0

,我建議你把眼光放在cocoacontrols.com因爲有大量的自定義警告框可用。 https://www.cocoacontrols.com/search?q=alert –

+0

你可以把一些代碼如何創建此警報或按鈕的整個方法 –

回答

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

非常感謝!!!!! –

相關問題