簡體   English   中英

將NSNotification發布到新的ViewController

[英]Post NSNotification to new ViewController

我有5個選項卡的選項卡式應用程序。

應用程序在索引為0的標簽上啟動

當我的應用程序收到推送通知時,我想在索引為1的選項卡中推送新的視圖控制器。

我的代碼:

AppDelegate

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {
    UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
    tabb.selectedIndex = 1;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushImage" object:@"this is my item id from pushData"];
}

ProfileViewController(標簽索引1)

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

-(void) pushImage: (NSNotification*) notification {
    NSString* text = notification.object;
    NSLog(@"My id from pushData: %@", text);
}

我的問題是,當AppDelegate觸發通知時,由於尚未完成初始化,所以ProfileViewController無法響應該通知。

如果手動打開選項卡1並再次切換回選項卡0,然后發布通知,則它會對此做出完美響應。 所以我需要在標簽1加載后發布通知,我該如何處理?

我在TabBarApplication中從AppDelegate推送新VC的解決方案

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {    
            // ...... 

            if([[pushData objectForKey:@"type"] integerValue] == 0){
                // ....
            }
            else if([[pushData objectForKey:@"type"] integerValue] == 2){
                [self handleLikePush:pushData applicationState:application.applicationState];
            }
}

-(void)handleLikePush:(NSDictionary *)pushData applicationState:(UIApplicationState) applicationState{

    //..

    DetailImageViewController *detailImage = [[DetailImageViewController alloc] initWithImageId:imageId];
        [self pushViewControllerToCurrentTab:detailImage];
    }

}

- (void)pushViewControllerToCurrentTab:(UIViewController *)vc{

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *selectedTabNC = (UINavigationController *)tabBarController.selectedViewController;

    if (selectedTabNC != nil) {
        [selectedTabNC pushViewController:vc animated:YES];
    }
    else{
        NSLog(@"NavigationController not found");
    }    
}

您可以使用

addObserver:instanceOfOtherClass

而不是addObserver:self

在appDelegate中添加以下行:

ProfileViewController *pvController=[ProfileViewController new];
[[NSNotificationCenter defaultCenter] addObserver:pvController selector:@selector(pushImage:) name:@"pushImage" object:nil];

這種方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {
      UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
      tabb.selectedIndex = 1;
      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushImage" object:@"this is my item id from pushData"];

     //****   add here

     ProfileViewController *pvController=[ProfileViewController new];
    //[[NSNotificationCenter defaultCenter] addObserver:pvController selector:@selector(pushImage:) name:@"pushImage" object:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushIamge" object:pvController];// userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 
}

您是否嘗試過將addObserver:方法添加到視圖控制器的init方法中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM