1

I want to do this below concept in my project:

I just created one small custom popup using UIViewController, this custom popup containing one message label and two button, one is "OK" and another one is "Cancel". This custom popup coding doing in appdelegate. Now when I want to open this popup, I just called this appdelegate popup method in from viewcontroller when I need to open this alert.

Now the problem is, I want to perform different-different functionality on "OK" button of "Custom alert" Popup. So please help to how to I managed this "Ok" Button click event from Individual ViewController.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Monika Patel
  • 2,287
  • 3
  • 20
  • 45
  • [take a look at this](https://github.com/Darktt/DTAlertView) – Fahim Parkar Apr 08 '17 at 05:38
  • I suggest you to look on cocoacontrols.com as there are plenty of custom alert boxes available. https://www.cocoacontrols.com/search?q=alert – Yahya Ibrahim Apr 08 '17 at 05:44
  • Can you put some code how do you created this alert or the entire method of the cutom button – Inder Kumar Rathore Apr 08 '17 at 05:51
  • As a good practice you should have a dedicated class to handle that. Anyway if you really want to do it from scratch you are after showing modally a dialog. For starters check out: http://stackoverflow.com/questions/12741224/ios-modal-viewcontroller-with-transparent-background http://stackoverflow.com/questions/16230700/display-uiviewcontroller-as-popup-in-iphone https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html – Kamil.S Apr 08 '17 at 06:17

1 Answers1

1

Put the below method in a Utility class and call it from your view controller like

[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)
 }
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184