将NSNotification发布到新的ViewController

时间:2013-01-14 11:30:08

标签: ios uitabbarcontroller push-notification nsnotificationcenter nsnotification

我有带有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);
}

我的问题是ProfileViewController无法响应通知,因为当AppDelegate触发通知时,初始化尚未完成。

如果手动打开标签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");
    }    
}

2 个答案:

答案 0 :(得分:4)

您可以使用

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"]]; 
}

答案 1 :(得分:1)

您是否尝试将addObserver:方法添加到视图控制器的init方法中?