简体   繁体   English

添加观察者UITableViewController的条形按钮?

[英]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.

相关问题 在 UITableViewController (Swift) 顶部添加按钮 - Add button on top of UITableViewController (Swift) 将任意定位的按钮添加到UITableViewController - Add arbitrarly positioned button to a UITableViewController 将导航栏添加到UITableViewController而不使用NavigationController - add navigation bar to UITableViewController without using NavigationController 初学者:如何在UITableViewController上添加带按钮的顶栏 - Beginner: How to add top bar with buttons on UITableViewController 如何在静态单元格中向UITableViewController添加按钮? - How to add button to UITableViewController in static cells? 使用静态单元格在UITableViewController上添加按钮覆盖 - Add button overlay on UITableViewController with use static cells 从Storyboard在UITableViewController上添加带标题的导航栏而没有导航控制器 - Add Navigation Bar with title without Navigation controller on UITableViewController from Storyboard 如何在UITableViewController顶部的标题中添加按钮和搜索栏? - How to add buttons and search bar into header at top of UITableViewController? 如何在Interface Builder中向UITableViewController添加导航栏? - How do I add a Navigation Bar to a UITableViewController in Interface Builder? 在导航栏中使用UITableViewController - Using UITableViewController in a navigation bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM