TechChain
TechChain

Reputation: 8952

Common function for alert view?

I want to show alertview in my controller classes. So i have created a common function to show alert & respond to it's action buttons.

In Commonfunctions.swift

i have created one function like 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 a protocol in Commonfunctions.swift.

protocol alertDelegate
{
  func okAction(controller:UIViewController)
  func cancelAction(controller:UIViewController)
}

In controller class i have added this

class

MyController:UIViewController,UITableViewDelegate,UITableViewDataSource,CLLocationManagerDelegate,alertDelegate
{
  var delegate:alertDelegate! = nil
}

call back functions goes here

 func okAction(controller: UIViewController) {

    print("Ok Action")

  }
  func cancelAction(controller: UIViewController) {

    print("Cncel Action")

  }

and i am showing the alert below like this

  Constant.commonfunction.showActionAlertView(title: NSLocalizedString("Success", comment: ""), message: NSLocalizedString("CreateProperty_Alert_created", comment: ""), vc: self)

I am not able to call okAction & cancelAction methods. Tell how to implement call back.

Upvotes: 0

Views: 1530

Answers (3)

Ajay Singh Thakur
Ajay Singh Thakur

Reputation: 485

I use it by as class(static) method in my Utility Class.(Swift 5)

 class UtilityClass {

    class func showAlertControllerWith(title:String, message:String?, onVc:UIViewController , style: UIAlertController.Style = .alert, buttons:[String], completion:((Bool,Int)->Void)?) -> Void {

        let alertController = UIAlertController.init(title: title, message: message, preferredStyle: style)
        for (index,title) in buttons.enumerated() {
            let action = UIAlertAction.init(title: title, style: UIAlertAction.Style.default) { (action) in
                completion?(true,index)
            }
            alertController.addAction(action)
        }

        onVc.present(alertController, animated: true, completion: nil)
    }

}

and in your view controller

UtilityClass.showAlertControllerWith(title: "Error", message: "your message", onVc: self, buttons: ["OK"]) { (succes, index) in
    if index == 0 { // ok button tapped

    }
}

and if you want to use it as Action sheet

UtilityClass.showAlertControllerWith(title: "Error", message: "Custom message", onVc: self, style: UIAlertController.Style.actionSheet, buttons: ["ok","cancel","custom"]) { (succes, index)  in
    switch index {
    case 0:break // ok tapped
    case 2:break // custom tapped
    default:
        break
    }
}

Upvotes: 1

Rajat
Rajat

Reputation: 11127

Yo need to create an object of that delegate like this

var delegate: alertDelegate?

and with delegate you can call this functions like this

also you need to assign the delegate as self while calling this method showActionAlertView

like this 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 for Cancel Action Here
    delegate?.cancelAction(controller: vc)
}))

Edited

Do it like this

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 it like this

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

Upvotes: 2

Krutarth Patel
Krutarth Patel

Reputation: 3455

you can use this function to use globally

 func openPopUP(Title: String,Message: String, vc: UIViewController){
   let alert = UIAlertController(title: Title, message: Message, preferredStyle: UIAlertControllerStyle.Alert)
   alert.addAction(UIAlertAction(title: "OK", style:   UIAlertActionStyle.Default, handler: nil))
   vc.presentViewController(alert, animated: true, completion: nil)
}

and to use it

 openPopUP("Title", Message: "Hello", vc: self)

Upvotes: 1

Related Questions