如何处理自定义视图的按钮点击事件?

How to handle Button click event of custom view?

我想在我的项目中执行以下概念:

我刚刚使用 UIViewController 创建了一个小的自定义弹出窗口,这个 custom popup 包含一个消息标签和两个按钮,一个是“确定”,另一个是“取消”。 custom popup 编码在 appdelegate 中进行。现在,当我想打开这个弹出窗口时,当我需要打开这个警报时,我只是在 viewcontroller 中调用了这个 appdelegate 弹出方法.

现在的问题是,我想在“自定义警报”弹出窗口的“确定”按钮上执行不同的功能。所以请帮助我如何管理来自个人 ViewController.

的这个“确定”按钮单击事件

将以下方法放入实用程序 class 并从您的视图控制器中调用它

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

Swift

 // 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)
 }