警报视图的常用功能?

时间:2016-11-08 08:34:28

标签: ios iphone swift

我想在我的控制器类中显示alertview。所以我创建了一个显示警报的常用功能。回应它的动作按钮。

Commonfunctions.swift

我创建了一个功能,如下所示

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

并在Commonfunctions.swift中创建了一个协议。

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

在控制器类中,我添加了这个

上课

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

回叫功能在这里

 func okAction(controller: UIViewController) {

    print("Ok Action")

  }
  func cancelAction(controller: UIViewController) {

    print("Cncel Action")

  }

我正在显示以下警告

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

我无法致电okAction& cancelAction方法。讲述如何实现回调。

3 个答案:

答案 0 :(得分:2)

哟需要像这样创建该委托的对象

showActionAlertView

并且使用委托你可以像这样调用这个函数

您还需要在调用此方法delegate = vc

时将委托指定为self

喜欢这个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) }))

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

<强>被修改

这样做

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

并像这样使用

.left{
  float:left
}

.right{
  float:right;
}

div{
  clear:both;
  width:400px;
}

答案 1 :(得分:1)

您可以使用此功能全局使用

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

并使用它

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

答案 2 :(得分:1)

我将其作为实用程序类中的类(静态)方法使用。(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)
    }

}

并在您的视图控制器中

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

    }
}

,如果您想将其用作操作表

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