在另一个控制器中显示/隐藏UIButton

MeV*_*MeV 0 uibutton ios appdelegate swift

我的ViewController.swift中有一个按钮:

@IBOutlet weak var exampleButton: UIButton!
Run Code Online (Sandbox Code Playgroud)

我想在AppDelegate中显示/隐藏该按钮,当特定事件发生时(即应用程序进入后台等).我怎样才能做到这一点?

Piy*_*rma 11

一种方法可以是 - 您可以使用通知

在视图控制器中添加Observer,其中需要隐藏按钮

NSNotificationCenter.defaultCenter().addObserver(
    self,
    selector: "hideButton",
    name: @"HIDE_BUTTON_NOTIFICATION",
    object: nil)



func hideButton() -> Void {
  // Hide your button here
  // Remember to hide on main thread
}
Run Code Online (Sandbox Code Playgroud)

无论您想要隐藏按钮的哪个位置(例如来自AppDelegate),您都可以发布此通知

 NSNotificationCenter.defaultCenter().postNotificationName(@"HIDE_BUTTON_NOTIFICATION", object: nil)
Run Code Online (Sandbox Code Playgroud)