微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用swift IOS使UIBarButtonItem消失

我有一个从故事板链接到的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")
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐