在我的主要应用类别中,我有<条码> 查询/编码>议定书,其中界定了<条码>应用DidBecomeActive方法。
我想在申请从背景返回时采用一种方法,但这种方法在另一种观点是控制器。 我如何检查一下在<编码>应用DidBecomeActive方法中目前显示的哪位控制器,然后对该控制器内的一种方法发出呼吁?
在我的主要应用类别中,我有<条码> 查询/编码>议定书,其中界定了<条码>应用DidBecomeActive方法。
我想在申请从背景返回时采用一种方法,但这种方法在另一种观点是控制器。 我如何检查一下在<编码>应用DidBecomeActive方法中目前显示的哪位控制器,然后对该控制器内的一种方法发出呼吁?
申请中的任何类别均可成为申请中不同通知的“观察员”。 当你创建(或装载)你的视力控制器时,你希望将其登记为<代码>的观察员。 UIApplication DidBecomeActiveNotification,并具体说明,当通知送交你申请时,你想要采用哪种方法。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
Don! 在你看来即将消失时,请删除其观察员:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
关于。
<>Swift 3, 4 Equivalent:
增加观察员
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
}
<>Swift 2 Equivalent:
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.
}
<>Swift 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
}
<>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个 Apple果通过新的汇编者警告说,我们避免在这种情形下使用<代码>#sctorele。 以下是实现这一目标的更安全途径:
首先,会产生一个可视旁听的变量(将用于取消观察员):
var didBecomeActiveObserver: NSObjectProtocol
之后,通知可使用 la:
lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
// Do stuff
}
如您要求列入实际通知,仅将<代码>_改为notification/code>。
其次,我们设立了这一通知,以观察这项通知是否积极。
func setupObserver() {
didBecomeActiveObserver = NotificationCenter.default.addObserver(
forName: UIApplication.didBecomeActiveNotification,
object: nil,
queue:.main,
using: didBecomeActive)
}
这里的重大变化是,我们现在不使用#selector
,而是称之为上文所创造的var。 这可以消除你失职的挑选人员坠毁的情况。
最后,我们排除了观察员。
func removeObserver() {
NotificationCenter.default.removeObserver(didBecomeActiveObserver)
}
<Swift 5 edition:
NotificationCenter.default.addObserver(self,
selector: #selector(loadData),
name: UIApplication.didBecomeActiveNotification,
object: nil)
移除观察员在9年及以后。
页: 1
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() {
}
The Combine way:
import Combine
var cancellables = Set<AnyCancellable>()
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
.sink { notification in
// do stuff
}.store(in: &cancellables)
www.un.org/Depts/DGACM/index_spanish.htm 更快速的5+解决办法
添加以下观察员:<条码>init或viewDidLoad
:
NotificationCenter.default.addObserver(self,
selector: #selector(appDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)
如其他答复所示,你不需要去除观察员。 自动完成。
@objc private func appDidBecomeActive() {
// do your magic
}
如果有的话,请迅速调查:
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.didBecomeActiveNotification)) { _ in
print("DID BECOME ACTIVE")
}
)
Swift5 MacOS, 你需要使用NSApplication,而不是UIApplication。
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: (NSApplication.didBecomeActiveNotification),
object: nil)
}
For a basic app with nonconsumable in-app purchases, has anyone figured out best practices for using SKPaymentQueue s restoreCompletedTransactions? Observations I know it s recommended to always ...
I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...
I have a UITextField that is a subview of a UITableViewCell. When my view loads, I want the text field to become first responder. I have a pointer to the text field in the table cell, so to do this I ...
I ve been working on adding in-app purchases and was able to create and test in-app purchases using Store Kit (yay!). During testing, I exercised my app in a way which caused the app to crash mid ...
In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...
Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...
I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...
Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...