2013-11-01 25 views
2

我已成功爲我的應用程序實施了推送通知。 我現在想要完成的是發送某種標誌到我的AppDelegate的根ViewController,以防應用程序收到PN。當接收推送通知後應用程序變爲活動狀態時,主ViewController上的觸發事件

我在applicationDidBecomeActive:開始通過檢查證件號碼,就像這樣:

if (application.applicationIconBadgeNumber>0) { 
     self.hasNotification = YES; 
     NSLog(@"APNs Message received");    
    } 

現在,我不知道如何將這一信息傳達給我的根ViewController以觸發賽格瑞,將採取用戶轉到其中一個視圖。什麼是最好的方法來解決這個問題?

回答

2

鑑於許多視圖控制器可能對此事件感興趣,這聽起來像是使用NSNotifications提供的發佈/訂閱模型的好候選者。

要發佈一個通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyEventName" object:optionalPayload]; 

要訂閱一個通知:

- (void)viewWillAppear:(BOOL)animated 
{  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) 
     name:@"MyEventName" object:nil]; 
} 

- (void)dealloc 
{ 
    //Unsubscribe yourself in dealloc 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

-(void)handleNotification:(NSNotification *)pNotification 
{ 
    NSLog(@"#1 received message = %@",(NSString*)[pNotification object]);  
    //Perform your segue here 
} 

替代方案:自定義根VC

如果您已經創建了擁有domai n專用容器視圖控制器作爲根視圖控制器,您可以執行以下操作:

  • 將事件發送到根視圖控制器。
  • 根視圖控制器會詢問其當前的子/子是否對該事件感興趣(可能通過標記協議)並傳播它。

我幾乎總是在我的應用程序中使用自定義容器 - RootViewController,因爲它可以導致很好的可讀代碼來描述發生了什麼。不僅如此,它還使得從這裏實現核心佈局的東西(例如滑動菜單等)變得非常簡單。

相關問題