简体   繁体   English

如何从UILocalNotification注册NSNotification?

[英]How to register for an NSNotification from a UILocalNotification?

I have a tabbar application and let's say that I want to switch to the second tab and popup an alert at 12:00, even if my application is not running. 我有一个tabbar应用程序,让我们说我想切换到第二个选项卡并在12:00时弹出警报,即使我的应用程序没有运行。

I got all the code for UILocalNotification working correctly, but then I thought that the best way to do that would be by posting a notification from the app delegate: 我得到了UILocalNotification的所有代码正常工作,但后来我认为最好的方法是通过发布应用代表的通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Add the tab bar controller's view to the window and display.
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    // Handle launching from a notification when the app is NOT running
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        [tabBarController setSelectedIndex:1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"AlertNotification" object:self];
    }
    return YES;
}

Then, in my SecondViewController.m, I have: 然后,在我的SecondViewController.m中,我有:

- (void)viewDidLoad {
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpAlert:) name:@"AlertNotification" object:nil];
}

But this does not work. 但这不起作用。 I suspect that the notification is sent while the viewDidLoad of the SecondViewController has not been called yet, right? 我怀疑在没有调用SecondViewController的viewDidLoad时发送通知,对吧? Is it possible to work this out? 有可能解决这个问题吗? And do you agree on my approach of using NSNotificationCenter in this case? 在这种情况下,您是否同意我使用NSNotificationCenter方法?

Thanks in advance. 提前致谢。

I quickly created a test project and got it working by putting the notification registration in awakeFromNib (assuming SecondViewController is created in a xib file) 我很快就创建了一个测试项目,并得到了其通过将通知登记工作awakeFromNib (假设SecondViewController在厦门国际银行文件创建)

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpAlert:) name:@"AlertNotification" object:nil];
}
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];

I guess, you are right. 我猜你是对的。 It doesn't work because you are posting the notification before adding the view controller as observer for it. 它不起作用,因为您在将视图控制器添加为观察者之前发布通知。

Another approach would be to add a bool property to the app delegate to indicate whether the app has been started from the local notification. 另一种方法是向应用程序委托添加bool属性,以指示应用程序是否已从本地通知启动。 The app delegate can be requested from anywhere in the app with [[UIApplication sharedApplication] delegate] . 可以使用[[UIApplication sharedApplication] delegate]从应用程序的任何位置请求应用程序[[UIApplication sharedApplication] delegate]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM