ios - How to handle Button click event of custom view? -
i want below concept in project:
i created 1 small custom popup using uiviewcontroller, custom popup containing 1 message label , 2 button, 1 "ok" , 1 "cancel". custom popup coding doing in appdelegate. when want open popup, called appdelegate popup method in viewcontroller when need open alert.
now problem is, want perform different-different functionality on "ok" button of "custom alert" popup. please how managed "ok" button click event individual viewcontroller.
please check attached screenshot [![enter image description here][1]][1]
thanks in advanced
put below method in utility class , call view controller like
[utility showalertwithtitle:@"abc" msg:@"msg" vc:self positivehandler:^(uialertaction *action) { // here when ok pressed } negativehandler:nil]; //pass nil when cancel 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 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) }
Comments
Post a Comment