使用swift IOS使UIBarButtonItem消失
发布时间:2020-12-14 02:29:54 所属栏目:百科 来源:网络整理
导读:我有一个从故事板链接到的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") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |