나는 tabbar 응용 프로그램을 가지고 있고 응용 프로그램이 실행 중이 지 않아도 두 번째 탭으로 전환하고 12:00에 경고를 팝업한다고 가정 해 봅시다.UILocalNotification에서 NSNotification에 등록하는 방법은 무엇입니까?
I UILocalNotification에 대한 모든 코드가 제대로 작동하지만, 그때는 애플 대리자에서 알림을 게시하여 그렇게 할 수있는 가장 좋은 방법이 될 것이라고 생각 가지고 : 내 SecondViewController.m에,
- (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;
}
다음 나는 가지고있다 :
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpAlert:) name:@"AlertNotification" object:nil];
}
그러나 이것은 작동하지 않는다. SecondViewController의 viewDidLoad가 아직 호출되지 않은 동안 통지가 전송되었다고 의심됩니다. 이것을 해결할 수 있습니까? 그리고이 경우에 NSNotificationCenter
을 사용하는 것에 대한 나의 접근 방식에 동의합니까?
미리 감사드립니다.
좋아요, 완벽하게 잘 작동합니다! 어떤 장소에서 -removeObserver를 호출 할 적절한 장소가 있습니까? 어쩌면 viewDidUnload 또는 dealloc? – phi
dealloc은 알림을 등록하기 바로 전에 – Jilouc
처럼 보입니다. 결과적으로 매번 새로운 등록을하게됩니다. –