Вопрос

I have Tabbed Application with 5 tabs.

App starts on tab with index 0

When my app recive push notification, i want to push new view controller in tab with index 1.

My code:

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 (tab index 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);
}

My problem is that the ProfileViewController can not response to the notification, because the initialisation not already done, when the AppDelegate fire the notification.

If a manually open the tab 1 and switch back to tab 0 again, an then post the notification, it perfectly respons to it. So i need to post notification after the tab 1 is loaded, how can i hand this?

My solution of pushing new VC from AppDelegate in TabBarApplication

    - (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");
    }    
}
Это было полезно?

Решение

You can use

addObserver:instanceOfOtherClass

instead of addObserver:self

In appDelegate add these lines :

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

to this method

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

Другие советы

Have you tried adding the addObserver: method to your view controller's init method?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top