Note that there are some explanatory texts on larger screens.

plurals
  1. POPost NSNotification to new ViewController
    primarykey
    data
    text
    <p>I have Tabbed Application with 5 tabs.</p> <p>App starts on tab with index 0</p> <p>When my app recive push notification, i want to push new view controller in tab with index 1.</p> <p>My code:</p> <p>AppDelegate</p> <pre><code>- (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"]; } </code></pre> <p>ProfileViewController (tab index 1)</p> <pre><code>- (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); } </code></pre> <p>My problem is that the ProfileViewController can not response to the notification, because the initialisation not already done, when the AppDelegate fire the notification.</p> <p>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? </p> <p>My solution of pushing new VC from AppDelegate in TabBarApplication</p> <pre><code> - (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"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload