2010-12-29 2 views
3

나는 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을 사용하는 것에 대한 나의 접근 방식에 동의합니까?

미리 감사드립니다.

답변

3

나는 신속하게 테스트 프로젝트를 생성하고 (SecondViewController 가정하면 XIB 파일에 생성됩니다)

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

좋아요, 완벽하게 잘 작동합니다! 어떤 장소에서 -removeObserver를 호출 할 적절한 장소가 있습니까? 어쩌면 viewDidUnload 또는 dealloc? – phi

+0

dealloc은 알림을 등록하기 바로 전에 – Jilouc

+0

처럼 보입니다. 결과적으로 매번 새로운 등록을하게됩니다. –

0

내 생각에, 당신 말이 맞습니다. 뷰 컨트롤러를 옵저버로 추가하기 전에 알림을 게시하기 때문에 작동하지 않습니다.

또 다른 방법은 응용 프로그램 대리인에게 bool 속성을 추가하여 응용 프로그램이 로컬 알림에서 시작되었는지 여부를 나타내는 것입니다. 앱 위임은 [[UIApplication sharedApplication] delegate]으로 앱의 어느 곳에서나 요청할 수 있습니다.

+0

awakeFromNib에 알림 등록을 넣어 작업을 얻었다하지만 앱이에서 시작되어 있는지 알고 현지 통지. 내 문제는 두 번째 탭 메서드를 호출하는 방법입니다. 이제 뭔가를하려고 노력하고있어 \t \t [(SecondViewController *) tabBarController.selectedViewController popUpAlert : nil]; 하지만 운이 없다면 지금까지는 – phi

+0

오, 알겠습니다. - 앱 위임자의 부울 값이 YES로 설정된 경우에만 내 SecondViewController -viewDidAppear에서 popUpAlert를 호출하겠습니까? – phi

+0

네, 그게 효과가 있습니다. – Felix

0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil]; 
[[UIApplication shareApplication] registerUserNotificationSettings: settings]; 
+2

코드에서 사용자의 질문을 처리하는 방법에 대한 설명을 제공 할 수 있습니까? – Suever

관련 문제