[英]Add observers of UITableViewController bar button?
I have a ViewController
that starts a number of NSTimers
. 我有一个ViewController
可以启动多个NSTimers
。 I add each of these NSTimers
as an observer to UIApplicationDidEnterBackgroundNotification
so that I can stop them if the application goes into the background. 我将每个这些NSTimers
作为观察者添加到UIApplicationDidEnterBackgroundNotification
以便在应用程序进入后台时停止它们。 Works well. 效果很好。
[[NSNotificationCenter defaultCenter]
addObserver:anotherTimer
selector:@selector(goBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
....
- (void) goBackground {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self invalidate];
}
Here's the problem: I also have subclassed UITableViewController
to add a Bar Button ("I" for information) to each of my ViewControllers. 问题出在这里:我还UITableViewController
,为每个ViewController添加了一个Bar Button(“ I”代表信息)。 The Bar Button opens another ViewController
which shows information about the app. 条形按钮会打开另一个ViewController
,其中会显示有关该应用程序的信息。 Just like when the app goes into the background, I want all the NSTimers
to stop when the user taps the Bar Button. 就像当应用程序进入后台一样,我希望所有NSTimers
在用户点击“条形按钮”时停止。
Is there a way to make the NSTimers
observers of when the user taps the Bar Button or when the ViewController
exits? 有没有一种方法可以使NSTimers
成为用户何时NSTimers
Bar Button或ViewController
退出时的观察者?
The answer was much easier than I thought. 答案比我想的要容易得多。 The correct way to do this is to create a new notification with an arbitrary name (eg, "infoButtonTapped") and add all NSTimers as an observer to that notification. 正确的方法是创建一个具有任意名称的新通知(例如“ infoButtonTapped”),并将所有NSTimers作为观察者添加到该通知中。 The notification calls the same method as the notification sent when the application goes into the background ("goBackground"): 该通知调用与应用程序进入后台时发送的通知相同的方法(“ goBackground”):
[[NSNotificationCenter defaultCenter]
addObserver:aTimer
selector:@selector(goBackground)
name:@"infoButtonTapped"
object:nil];
The method called when the user taps the info Bar Button posts the notification with the same name: 用户点击信息栏按钮时调用的方法以相同的名称发布通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"infoButtonTapped" object:self];
Then all the NSTimers' goBackground methods will be called when the info Bar Button is pressed. 然后,当按下信息栏按钮时,将调用所有NSTimers的goBackground方法。
Note that I created a Category for NSTimer which includes setObservers, removeObservers, and goBackground methods which handle the notifications work very nicely. 请注意,我为NSTimer创建了一个Category,其中包括setObservers,removeObservers和goBackground方法,它们可以很好地处理通知。
I should also note that whenever one of the NSTimers is invalidated (whether in goBackground or elsewhere), it must first be removed as an observer to all notifications: 我还应该注意,无论何时一个NSTimers失效(无论在goBackground还是在其他地方),都必须首先将其作为观察者删除所有通知:
[aTimer removeObservers];
[aTimer invalidate];
Otherwise, the app will crash when one of these notifications is sent... 否则,当发送这些通知之一时,应用程序将崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.