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

时间:2018-04-11 05:37:55

标签: ios swift alert

我的下面的代码会触发警报,但是我想触发警报并仅显示操作,具体取决于条件,例如在下面的条件中,如果结果为真,那么我只想显示警报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
    })

3 个答案:

答案 0 :(得分:0)

如果您想有条件地展示UIAlertAction。如果您的条件为真,则表示您希望显示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 }

如下图所示。

enter image description here

答案 1 :(得分:0)

让我们说这是你的警报控制器

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)

答案 2 :(得分:0)

你可以像这样使用:

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)