【问题标题】:change UIButtonOutlet Image from AppDelegate从 AppDelegate 更改 UIButtonOutlet 图像
【发布时间】:2015-12-02 22:32:09
【问题描述】:

我想通过按钮图像让用户知道何时有新内容可用。以 snapchat 为例:用户知道新的快照是因为左按钮变为红色(在 mainView 上)。我也想这样做。 我已经在后台处理了通知 didReceiveRemoteNotification。感谢this post,当新的 APN 到达时,我能够在后台唤醒应用程序。

现在我想直接从applicationWillEnterForeground 更新按钮的图像。这是我想到的唯一方法,因为当用户点击应用(不是横幅或通知中心)时,视图已经加载。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: swift appdelegate


    【解决方案1】:

    如果有人遇到同样的问题...

    尝试直接与 AppDelegate 中的 ViewController 交互显然是一个糟糕的主意。相反,我遇到了this post,它告诉我如何知道 ViewController 何时进入前台。这是@nsgulliver 的答案:

    override func viewDidLoad() {
        super.viewDidLoad()
    
         NSNotificationCenter.defaultCenter().addObserver(self, selector:"doYourStuff", name:
         UIApplicationWillEnterForegroundNotification, object: nil)
     }
    
     func doYourStuff(){
      // your code
     }
    

    然后移除观察者:

    deinit {
            NSNotificationCenter.defaultCenter().removeObserver(self)
           }
    

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多