In TabBarController + NavigationController mix env, how to reset nav when tab changes? - cocoa-touch

I am building an app, which has TabBarController, and each tab view has navigation view controller. When user click tab, I'd like the relavent navigation view controller to "reset" to the root panel.
In my code, I uses the following way to initialize the tab and navigation controller.
viewController1 = [[MyFirstController alloc] init];
UINavigationController *tableNavController = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
viewController2 = [[MySecondController alloc] init];
UINavigationController *table2NavController = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:tableNavController, table2NavController, nil];
Then appears that all the tab and navigation controller is working automatically. I am not sure where to cut in to let navigation controller view to reset when it is selected.
Thanks.

The navigation controller is sent a viewWillAppear message by the system, so you can implement the method to do the resetting..
Another option would be to use the tab bar controller's selectedViewController method to retrieve the selected navigation controller..
Hope that helps..

Related

How to access Tab View Controller from another View Controller

I'm having trouble accessing my view controllers under the tab bar controller. Here is what my storyboard looks like:
View Controller A (-> Page View Controller -> View Controller C
View Controller A -> Tab Bar Controller (MyTabBarController.h/.m) -> Navigation Controller (MyNavigationController.h/.m)-> View Controller B (TabViewController.h/.m)
Tab Bar Controller (MyTabBarController.h/.m) -> View Controller D
Tab Bar Controller (MyTabBarController.h/.m) -> View Controller E
From View Controller A I have an IBAction called loginButton that is connected to the Tab Bar Controller, and currently it looks like this:
- (IBAction)loginButton:(id)sender {
MyNavigationController *localNavigationController;
UIStoryboard * storyboard = self.storyboard;
MyTabBarController *tbc = [[MyTabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1];
TabViewController *login = [storyboard instantiateViewControllerWithIdentifier: # "TabViewController"];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:login];
localNavigationController.delegate = self;
[localControllersArray addObject:localNavigationController];
tbc.viewControllers = localControllersArray;
tbc.delegate = self;
tbc.moreNavigationController.delegate = self;
tbc.selectedIndex = 0;
[self presentViewController:tbc animated:YES completion:^{
}];
}
I'm not able to get this displayed correctly. I am getting a bunch of warnings in this piece of code. and it is also not showing the different tab items in the bottom of the Tab Bar, even though I have put images/text on each tab.
So how do I display/access the view controllers inside the Tab Bar Controller correctly? (ie View Controllers C/D/E)?
The storyboard that you show in your question already contains the tab bar controller, navigation controller, and login controller properly hooked up to each other. Because of that, you shouldn't be instantiating a new tab bar controller or navigation controller in code -- they will be instantiated by the storyboard when you instantiate the tab bar controller. So, the only thing you need to do, is to give the tab bar controller in the storyboard an identifier, and do this (assume the identifier is called MyTabBarController):
- (IBAction)loginButton:(id)sender {
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyTabBarController"];
[self presentViewController:tbc animated:YES completion:nil];
}
You wouldn't even need this code if you control drag from the "Login" button to the tab bar controller, and choose "Modal". That will create a modal segue which will present the tab bar controller with no code at all.
If you just want to select another tab from the tabBar controller then use something like this:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:3];
Note that if the tabBar controller is the initial view controller you can grab an instance of it it the applicationDidFinishLaunching method and store it in the AppDelegate. Then you'll be able to access it like this:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Remember to import the AppDelegate.h
I recommand you to use a Singleton shared instance to share multiple informations form multiple controllers.
It's a good design Pattern for you usage.
I'm writing samples of Design Patterns usage on cocoa (see https://github.com/leverdeterre/DesignPatterns -> Real singleton)

Xcode open specific view with push notifications in storyboard

In my codes, I wrote it in this way
DetailView *vc = (DetailView *)[mainStoryboard instantiateViewControllerWithIdentifier:#"DetailVC"];
self.window.rootViewController = vc;
It only show that view but I don't see the navigational bar and tab bar.
What is the right way to open a specific view (inside tabbarcontroller) within storyboard when the app receive remote notifications automatically?
That's because you get only DetailView from storyboard. If you want to show it inside navigation controller you have to init this controller.
DetailView *vc = (DetailView *)[mainStoryboard instantiateViewControllerWithIdentifier:#"DetailVC"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController: vc];
self.window.rootViewController = navVC;
But I think the issue is in other. You instantiate wrong view controller. You should instantiate tab bar controller or navigation controller and then only select proper tab.

How to get the Xcode TabView default black bar in bottom?

In a new tabbed app application when two more views are added, views which are created by default has a black bar on bottom and the two views that are manually created doesn't have that black bar ?
How to enable that black bar ?
You need to connect your two other scenes to the tab bar controller. Hold the control key down, and drag from Tab Bar View Controller to the other View Controller. When it asks what type of a segue you want to create, choose "view controllers" under the "Relationship" segue section.
If you are using NIBs, drag a view controller object from the object library into the tab bar controller. Once it's there, select the new view controller in the view controller explorer and configure the Custom Class in the Identity Inspector.
... and the name of the nib in the Attributes Inspector.
In your app delegate you should see code like this:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
Modify it to include your third and fourth view controllers. Otherwise, how would your tab controller know your new view controllers exist!

rightBarButtonItem does not appear in Navigation Bar iOS

I'm having problems displaying the rightBarButtonItem of the Navigation Bar - I'm attempting to create it programmatically in the Application Delegate, where my UINavigationController is set up.
Code is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
RSCListViewController *list = [[RSCListViewController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc] initWithRootViewController:list];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"+"
style:UIBarButtonItemStylePlain
target:list
action:#selector(addPressed:)];
self.navController.navigationItem.rightBarButtonItem = barButton;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[DatabaseManager openDatabase];
return YES;
}
Running the application, no button item appears on the navigation bar.
I'm not sure whether I have missed something obvious - my attempts to rectify the problem using related Stack Overflow threads haven't yielded any success.
Any help appreciated.
You need to attach your bar button item to your custom view controller, not to the navigation controller. From Updating the Navigation Bar:
In addition, the navigation controller object builds the contents of
the navigation bar dynamically using the navigation items (instances
of the UINavigationItem class) associated with the view controllers on
the navigation stack. To change the contents of the navigation bar,
you must therefore configure the navigation items for your custom view
controllers.
(...)
The navigation controller updates the right side of the navigation bar
as follows:
If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button
item, set the rightBarButtonItem property of the view controller’s
navigation item.
If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.
Therefore, replace:
self.navController.navigationItem.rightBarButtonItem = barButton;
with:
list.navigationItem.rightBarButtonItem = barButton;

Several Navigation Controllers in an app

I have an app with navigation controller, but my app also has a simple separate view which is not a navigation controller part. And what I want to do is to add a brand new navigation controller to this view.
For my first navigation controller I used this code in my AppDelegate:
UINavigationController *navigationController = [[UINavigationController new] initWithRootViewController:viewController1];
navigationController.viewControllers = [NSArray arrayWithObject:viewController1];
self.window.rootViewController = navigationController;
But which code I should use, if I want to create a new Navigation Controller ?
Thanks !
UPDATE:
So, I made some pictures:
One the first pic there is a navigation controller (which is declared in AppDelegate). And It contains an info button. Then, when we press the button we move to another view (pic 2). And its just a navigation bar in this view, not an navigation controller. I wanna add a navigation controller, not a navigation bar in this view, so users will be able to use UITableView easily
We would need to know a little more about your intended view hierarchy, and application flow. But in the code you're posting, I'm not sure what is going on. Why not just:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.window.rootViewController = navController;
That said, you would create other UINavigationControllers exactly the same way.
Update:
When you press the info button on the first view, you could present the navigation controller modally I think.
- (IBAction)infoButtonAction:(id)sender;
{
// InfoTableViewController is the controller with Instructions, Contact, and something else
// my Russian isn't so good.
InfoTableViewController *tableController = [[InfoTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableController];
navController.modalPresentationStyle = UIModalPresentationFormSheet; // or whatever
[self presentModalViewController:navController animated:YES];
}
Note that I'm not dealing with memory management because I don't know if this is ARC or not. Is that how it should behave?