I'm newbie in iOS App Dev. I'm working on application having tabbar whose default tabindex is 2. My problem is: When push notification arrive (in background state of app) after clicking on notification, my app open up on tab 2 and show an alertview. After clicking button of alertview I want to change the selectedTab to 3. Im writing below code in appDelegate in alertView:clickedButtonAtIndex:, everything works fine.
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"story_iphone" bundle:nil];
TabBarController * tb = [sb instantiateViewControllerWithIdentifier:@"TabBarController"];
[[tb.viewControllers firstObject] viewWillAppear:YES];
[tb viewWillAppear:YES];
[tb setSelectedIndex:3];
self.window.rootViewController = tb;
But im facing following problems:
1) ViewWillAppear of that VC (say vc1) is not called.
2)Also after pushing vc2 from vc1, viewWillAppear of VC2 is not called.
3) Poping back to VC1, ViewDidDisappear, ViewWillDisappear of VC2 is not called.
I have searched a lot but im unable to get where im lacking. Please help me. Thanks in Advance.
Assuming UINavigationController is rootViewController of your app and you are pushing TabBarController to its navigation stack, this should work fine
EDIT
From your comments its clear that, you have tab bar controller in your navigation stack at position 1 rather than at 0 :) Hence now look for viewController at index 1.
(self.window?.rootViewController as! UINavigationController).viewControllers[1] as! UITabBarController).selectedIndex = your_index_value
Suggestion :
Though above mentioned solution works by the virtue that, tab bar controller is at index 1, but the position of tab bar controller in navigation stack may vary at some point if you decide to modify navigation stack itself.
Rather than hard coding the index value, what you can do is, in your app delegate have a variable named tab bar controller and make it public :) now once you create a tab bar controller before pushing it to navigation stack now hold the reference to it :)
Because app delegate is singleton and public property of it is accessible to all the viewControllers, now you can access it directly using app delegate.tabBarController :)
Now who cares where is it navigation stack :) Now you have the reference to the tab bar use it happily :) Happy coding :)