2017-04-08 115 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

非常感谢!!!!! –

相关问题