加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用swift IOS使UIBarButtonItem消失

发布时间:2020-12-14 05:49:17 所属栏目:百科 来源:网络整理
导读:我有一个我从故事板链接到的IBOutlet @IBOutlet var creeLigueBouton: UIBarButtonItem! 如果条件成立,我想让它消失 if(condition == true){ // Make it disappear} 你真的想隐藏/展示creeLigueBouton吗?相反,启用/禁用UIBarButtonItems要容易得多。您可
我有一个我从故事板链接到的IBOutlet
@IBOutlet var creeLigueBouton: UIBarButtonItem!

如果条件成立,我想让它消失

if(condition == true)
{
    // Make it disappear
}
你真的想隐藏/展示creeLigueBouton吗?相反,启用/禁用UIBarButtonItems要容易得多。您可以使用以下几行来完成此操作:
if(condition == true) {
    creeLigueBouton.enabled = false
} else {
    creeLigueBouton.enabled = true
}

这段代码甚至可以用更短的方式重写:

creeLigueBouton.enabled = !creeLigueBouton.enabled

让我们在UIViewController子类中看到它:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var creeLigueBouton: UIBarButtonItem!

    @IBAction func hide(sender: AnyObject) {
        creeLigueBouton.enabled = !creeLigueBouton.enabled
    }

}

如果您真的想显示/隐藏creeLigueBouton,可以使用以下代码:

import UIKit

class ViewController: UIViewController {

    var condition: Bool = true
    var creeLigueBouton: UIBarButtonItem! //Don't create an IBOutlet

    @IBAction func hide(sender: AnyObject) {
        if(condition == true) {
            navigationItem.rightBarButtonItems = []
            condition = false
        } else {
            navigationItem.rightBarButtonItems = [creeLigueBouton]
            condition = true
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        creeLigueBouton = UIBarButtonItem(title: "Creer",style: UIBarButtonItemStyle.Plain,target: self,action: "creerButtonMethod")
        navigationItem.rightBarButtonItems = [creeLigueBouton]
    }

    func creerButtonMethod() {
        print("Bonjour")
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读