【问题标题】:Setting reference to UIButton globally全局设置对 UIButton 的引用
【发布时间】:2020-10-28 20:51:15
【问题描述】:

所以我有一个 UIButton 添加到窗口中,因此它可以在任何呈现的控制器上可见,并且它具有我在其间设置动画的各种状态。

let window = UIApplication.shared.keyWindow!
window.addSubview(customButton)

这很好用,但是我想在应用程序中的特定视图中显示/隐藏它,我正在努力寻找解决这个问题的最佳方法。将其添加为全局参考会非常不受欢迎吗?

let customButton: CustomButton = CustomButton()

class NavigationClass: UINavigationController {
.....
}

这似乎是最简单的方法,因为我可以从任何类访问它...

class ControllerClass: UIViewController {
  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        customButton.isHidden = true
        customButton.resetState()
    }
}

有什么理由我不应该这样做,或者有更好的方法来处理它吗?

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    如果需要,您可以使用静态按钮创建 ParentViewController 并为所有子控制器实现行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 2016-06-04
      • 2013-01-09
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多