swift: iOS 根据条件显示警报操作

swift: iOS show alert action depending to a condition

我的代码下面会触发警报,但我想触发警报并仅根据条件显示操作,例如在下面的条件中,如果结果为真,那么我只想显示警报 1,否则显示警报 2

根据条件显示警报操作

var a = loggedInUsername

if ((a?.range(of: "mother")) != nil) {
    print("true")
    print ("name:" , loggedInUsername)
    let action1 = UIAlertAction(title: "Delete", style: .default, handler: { (action) -> Void in
        print("ACTION 1 selected!")
    })

    let action2 = UIAlertAction(title: "Approve Chore", style: .default, handler: { (action) -> Void in
    })

如果你想显示 UIAlertAction conditionally.Like 如果你的条件为真你想显示 action1 如果条件为假你想显示 action2 .

试试这个。

let alert = UIAlertController(title: AppName, message: "YOUR MESSAGE", preferredStyle: .alert)
    alert.view.tintColor = Colors.NavTitleColor
    let action1 = UIAlertAction(title: "Delete", style: .default, handler: {(_ action: UIAlertAction) -> Void in

    })
    let action2 = UIAlertAction(title: "Approve Chore", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in

    })

    if ((a?.range(of: "mother")) != nil) {
        alert.addAction(action1)
    }
    else {
        alert.addAction(action2)
    }

    present(alert, animated: true) {() -> Void in }

如果您想在 UIAlertAction 标题前添加图片,请使用以下代码。

let alert = UIAlertController(title: "Title", message: "YOUR MESSAGE", preferredStyle: .alert)
    alert.view.tintColor = Colors.NavTitleColor

    let image1 = UIImage(named: "attendance")
    let action1 = UIAlertAction(title: "Delete", style: .default, handler: nil)
    action1.setValue(image1, forKey: "image")

    let image2 = UIImage(named: "mail")
    let action2 = UIAlertAction(title: "Approve Chore", style: .default, handler: nil)
    action2.setValue(image2, forKey: "image")

    alert.addAction(action1)
    alert.addAction(action2)

    present(alert, animated: true) {() -> Void in }

看起来像下图。

假设这是您的警报控制器

let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)

用于显示动作的布尔变量

var shouldShowAction1 = true //change it programmatically based on your requirement

你的行动

    let action1 = UIAlertAction(title: "Delete", style: .default, handler: { (action) -> Void in
        print("ACTION 1 selected!")
    })

    let action2 = UIAlertAction(title: "Approve Chore", style: .default, handler: { (action) -> Void in
    })

根据条件向警报添加操作

if shouldShowAction1{
   alert.addAction(action1)
}else{
   alert.addAction(action2)
}

显示警报

self.present(alert, animated: true, completion: nil)

你可以这样使用:

var isShowWhichAction:Int = 0

let alert = UIAlertController(title: "My Alert", message: "Your Message Here", preferredStyle: .alert)

let action1 = UIAlertAction(title: "Delete", style: .default, handler: { (action) -> Void in
        print("ACTION 1 selected!")
    })

    let action2 = UIAlertAction(title: "Approve Chore", style: .default, handler: { (action) -> Void in
    })

if isShowWhichAction == 1{ // change the condition it according to your requirement
 alert.addAction(action2) //Condition True
}else{
 alert.addAction(action1) //Condition False
}

这样使用:

    var a = loggedInUsername
    if ((a?.range(of: "mother")) != nil) {
        print ("name:" , loggedInUsername)
        isShowWhichAction = 1
    }else{
       isShowWhichAction = 0
    }

希望对您有所帮助

编辑

添加图片:

var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)