私はタブバーアプリケーションを持っていて、アプリケーションが実行されていなくても、2番目のタブに切り替えて12時にアラートをポップアップしたいとしましょう。UILocalNotificationからNSNotificationを登録するには?
私が正しく動作し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;
}
その後、私のSecondViewController.mに私は持っています:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpAlert:) name:@"AlertNotification" object:nil];
}
しかし、これは動作しません。 SecondViewControllerのviewDidLoadがまだ呼び出されていない間に通知が送信されたと思われます。これを行うことは可能ですか?この場合、NSNotificationCenter
を使用する私のアプローチに同意しますか?
ありがとうございます。
偉大な、それは完璧に動作します! -removeObserverを呼び出す適切な場所はどこですか?たぶん、viewDidUnloadまたはdealloc? – phi
deallocは通知を登録する直前に – Jilouc
と思われます。その結果、毎回新しい登録が行われます。 –