Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何处理自定义视图的按钮点击事件?_Ios_Objective C_Custom Controls - Fatal编程技术网

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

Ios 如何处理自定义视图的按钮点击事件?,ios,objective-c,custom-controls,Ios,Objective C,Custom Controls,我想在我的项目中实现以下理念: 我刚刚使用UIViewController创建了一个小型自定义弹出窗口,这个自定义弹出窗口包含一个消息标签和两个按钮,一个是“OK”,另一个是“Cancel”。此自定义弹出窗口在appdelegate中进行编码。现在,当我想打开这个弹出窗口时,当我需要打开这个警报时,我在fromviewcontroller中调用了这个appdelegate弹出方法 现在的问题是,我想在“自定义警报”弹出窗口的“确定”按钮上执行不同的功能。因此,请帮助我如何从单个ViewContr

我想在我的项目中实现以下理念:

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

现在的问题是,我想在“自定义警报”弹出窗口的“确定”按钮上执行不同的功能。因此,请帮助我如何从单个ViewController管理此“确定”按钮单击事件

请检查我的附加屏幕截图 [![在此处输入图像描述][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

迅捷的


我建议您访问cocoacontrols.com,因为有很多定制警报框可用。你能写一些代码吗?你是如何创建这个警报或者cutom按钮的整个方法的?一个好的做法是,你应该有一个专门的类来处理这个问题。无论如何,如果你真的想从头开始,你是在显示一个对话框之后。对于初学者,请查看:
+ (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)
 }