Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift:iOS根据条件显示警报动作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的下面的代码会触发警报,但我想触发警报并仅显示操作,具体取决于条件,例如在下面的条件中,如果结果为真,那么我只想显示警报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",handler: { (action) -> Void in
    })

解决方法

如果你想有条件地显示UIAlertAction.如果你的条件是真的,你想要显示action1,如果条件是假,你想要显示action2.

试试这个.

let alert = UIAlertController(title: AppName,message: "YOUR messaGE",preferredStyle: .alert)
    alert.view.TintColor = Colors.NavtitleColor
    let action1 = UIAlertAction(title: "delete",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标题之前添加Image而不是使用下面的代码.

let alert = UIAlertController(title: "title",preferredStyle: .alert)
    alert.view.TintColor = Colors.NavtitleColor

    let image1 = UIImage(named: "attendance")
    let action1 = UIAlertAction(title: "delete",handler: nil)
    action1.SETVALue(image1,forKey: "image")

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

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

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

看起来像下图.

@L_801_12@

大佬总结

以上是大佬教程为你收集整理的swift:iOS根据条件显示警报动作全部内容,希望文章能够帮你解决swift:iOS根据条件显示警报动作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。