首页 > 解决方案 > 如何禁用另一个类的按钮?

问题描述

我有两个带有两个按钮的 UIViewController。

如何从第一个类/UIViewController 中禁用第二个按钮?

谢谢。

标签: iosswiftclassbuttonuiviewcontroller

解决方案


类视图控制器 1

class ViewController: UIViewController {
@IBAction func btn1(_ sender: UIButton) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Disable"), object: nil)
}

}

类视图控制器 2

class ViewController2: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController2.disableBtn), name: NSNotification.Name(rawValue: "Disable"), object: nil)
}
@IBOutlet weak var btn2: UIButton!
@objc func disableBtn() {
    btn2.isEnabled = false
}

}


推荐阅读