ios - Common function for alert view? -
i want show alertview in controller classes. have created common function show alert & respond it's action buttons.
in commonfunctions.swift
i have created 1 function below
func showactionalertview(title:string,message:string,vc:uiviewcontroller) -> void { let alert = uialertcontroller(title: "warning", message: nslocalizedstring("alert_delete", comment: ""), preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "yes", style: .default, handler: { (action: uialertaction!) in constant.commonfunction.showloader(withmessage: "loading") })) alert.addaction(uialertaction(title: "cancel", style: .cancel, handler: { (action: uialertaction!) in print("handle cancel logic here") })) vc.present(alert, animated: true, completion: nil) }
and created protocol in commonfunctions.swift
.
protocol alertdelegate { func okaction(controller:uiviewcontroller) func cancelaction(controller:uiviewcontroller) }
in controller class have added
class
mycontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource,cllocationmanagerdelegate,alertdelegate { var delegate:alertdelegate! = nil }
call functions goes here
func okaction(controller: uiviewcontroller) { print("ok action") } func cancelaction(controller: uiviewcontroller) { print("cncel action") }
and showing alert below
constant.commonfunction.showactionalertview(title: nslocalizedstring("success", comment: ""), message: nslocalizedstring("createproperty_alert_created", comment: ""), vc: self)
i not able call okaction
& cancelaction methods. tell how implement call back.
yo need create object of delegate
var delegate: alertdelegate?
and delegate can call functions
also need assign delegate self while calling method showactionalertview
like delegate = vc
alert.addaction(uialertaction(title: "yes", style: .default, handler: { (action: uialertaction!) in constant.commonfunction.showloader(withmessage: "loading") delegate?.okaction(controller: vc) })) alert.addaction(uialertaction(title: "cancel", style: .cancel, handler: { (action: uialertaction!) in print("handle cancel logic here") // call delegate cancel action here delegate?.cancelaction(controller: vc) }))
edited
do
class utility : nsobject { var delegate: alertdelegate? func showactionalertview(title:string,message:string,vc:uiviewcontroller) -> void { let alert = uialertcontroller(title: "warning", message: nslocalizedstring("alert_delete", comment: ""), preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "yes", style: .default, handler: { (action: uialertaction!) in self.delegate?.okaction(controller: vc) })) alert.addaction(uialertaction(title: "cancel", style: .cancel, handler: { (action: uialertaction!) in print("handle cancel logic here") self.delegate?.cancelaction(controller: vc) })) vc.present(alert, animated: true, completion: nil) } }
and use
func showalert() { let vcutility = utility() vcutility.delegate = self vcutility.showactionalertview(title: "message", message: "message", vc: self) } func okaction(controller: uiviewcontroller) { print("ok") } func cancelaction(controller: uiviewcontroller) { print("cancel") }
Comments
Post a Comment