处理applicationDidBecomeActive - “视图控制器如何响应应用程序变为活动状态?”

时间:2010-09-03 22:01:57

标签: ios iphone ios4 multitasking uiapplicationdelegate

我的主AppDelegate.m类中有UIApplicationDelegate协议,并定义了applicationDidBecomeActive方法。

我想在应用程序从后台返回时调用方法,但该方法在另一个视图控制器中。如何检查当前在applicationDidBecomeActive方法中显示哪个视图控制器,然后调用该控制器中的方法?

11 个答案:

答案 0 :(得分:292)

应用程序中的任何类都可以成为应用程序中不同通知的“观察者”。当您创建(或加载)视图控制器时,您需要将其注册为UIApplicationDidBecomeActiveNotification的观察者,并指定在将通知发送到您的应用程序时要调用的方法。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(someMethod:)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];

别忘了自己清理!当你的观点消失时,请记得以观察者身份移除自己:

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIApplicationDidBecomeActiveNotification
                                              object:nil];

有关Notification Center

的更多信息

答案 1 :(得分:53)

Swift 3,4等效:

添加观察者

NotificationCenter.default.addObserver(self,
    selector: #selector(applicationDidBecomeActive),
    name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
    object: nil)

删除观察者

NotificationCenter.default.removeObserver(self,
    name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
    object: nil)

回调

@objc func applicationDidBecomeActive() {
    // handle event
}

答案 2 :(得分:16)

Swift 2等效

let notificationCenter = NSNotificationCenter.defaultCenter()

// Add observer:
notificationCenter.addObserver(self,
  selector:Selector("applicationWillResignActiveNotification"),
  name:UIApplicationWillResignActiveNotification,
  object:nil)

// Remove observer:
notificationCenter.removeObserver(self,
  name:UIApplicationWillResignActiveNotification,
  object:nil)

// Remove all observer for all notifications:
notificationCenter.removeObserver(self)

// Callback:
func applicationWillResignActiveNotification() {
  // Handle application will resign notification event.
}

答案 3 :(得分:5)

Swift 4.2

添加观察者-

NotificationCenter.default.addObserver(self, selector: #selector(handleEvent), name: UIApplication.didBecomeActiveNotification, object: nil)

删除观察者-

NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)

处理事件-

@objc func handleEvent() {
}

答案 4 :(得分:3)

使用Swift 4,Apple建议通过新的编译器警告我们避免在这种情况下使用#selector。以下是实现此目的的更安全的方法:

首先,创建一个可供通知使用的惰性var:

lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
    // Do stuff
} 

如果您需要包含实际通知,只需将_替换为notification

接下来,我们设置通知以观察应用程序是否处于活动状态。

func setupObserver() {
    _ = NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive,
                                               object: nil,
                                               queue:.main,
                                               using: didBecomeActive)
}

这里的重大变化是,我们现在调用上面创建的var,而不是调用#selector。这可以消除导致选择器崩溃的情况。

最后,我们删除了观察者。

func removeObserver() {
    NotificationCenter.default.removeObserver(self, name: .UIApplicationDidBecomeActive, object: nil)
}

答案 5 :(得分:1)

快速5

fileprivate  func addObservers() {
      NotificationCenter.default.addObserver(self,
                                             selector: #selector(applicationDidBecomeActive),
                                             name: UIApplication.didBecomeActiveNotification,
                                             object: nil)
    }

fileprivate  func removeObservers() {
        NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
    }

@objc fileprivate func applicationDidBecomeActive() {
// here do your work
    }

答案 6 :(得分:1)

组合方式:

import Combine

var cancellables = Set<AnyCancellable>()
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
    .sink { notification in
            // do stuff
    }.store(in: &cancellables)

答案 7 :(得分:1)

Swift 5 版本:

 NotificationCenter.default.addObserver(self,
                                               selector: #selector(loadData),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)

在iOS 9及更高版本中删除观察者is no longer required

答案 8 :(得分:1)

在 Swift 5 中

override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)

         NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
    
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        }
    
    override func viewWillDisappear(_ animated: Bool) { 
        super.viewWillDisappear(animated)

        NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)

        NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
    }


@objc private func applicationWillResignActive() {
    }

    @objc private func applicationDidBecomeActive() {
    }

答案 9 :(得分:0)

如果您使用的是SwiftUI:

.onReceive(NotificationCenter.default.publisher(
    for: UIApplication.didBecomeActiveNotification)) { _ in
        print("DID BECOME ACTIVE")
    }
)

答案 10 :(得分:0)

对于 Swift5 MacOS,您需要使用 NSApplication 而不是 UIApplication。

NotificationCenter.default.addObserver(self,
                                       selector: #selector(applicationDidBecomeActive),
                                       name: (NSApplication.didBecomeActiveNotification),
                                       object: nil)
    }