topviewcontroller for uitabbar - iphone

I have created a navigation based project with coredata and replaced the navigationcontroller with a tabbarcontroller.
But im finding it hard to rewrite this:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
Thanks.

UITabBarController doesn't have a root view controller because it doesn't manage a stack of view controllers, but rather the array (with view controllers ordered according to the slots occupied in the tab bar).
This snippet of code will set managed object context for the first (leftmost) view controller in tab bar controller:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)[tabBarController.viewControllers objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;

Related

TabbedView not displaying the Navigation bar

iOS beginner here. I'm Using XCode 4.6.3 and doing some tutorials. I have a question regarding a TabbedView not displaying the Navigation bar:
I set the Top Bar attribute to "Navigation Bar" here :
But it doesn't show here :
Below is the code in the AppDelegate :
self.navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.navController.navigationBar.barStyle = UIBarStyleBlack;
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
What am I doing wrong?
You have initialized your Navigation Controller with your First View Controller. So, you have to use Navigation Controller for the Tab Bar's View Controllers.
Change this Line
self.tabBarController.viewControllers = #[viewController1, viewController2];
With
self.tabBarController.viewControllers = #[self.navController, viewController2];
You have UITabBarController as rootViewController of your UINavigationController. And UINavigationController as root Controller of your app. Instead of that you have to set UITabBarController as root Controller of your App and add UINavigationController in each tab.
Check this answer.

Best practice when combining Tabs with Navigation

I used tabbed application template to start my new iPhone project.
I will have 4 tabs.
And I want to nave navigation bar at the top.
So I added this in AppDelegate:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller, viewController2, nil];
And this added navigation bar only at the top of the first ViewController (first tab).
What I want is to add navigation bar for the whole application not just in one view.
This will enable me to jump from one view to another and back across tabs.
So what is the best way to programmatically make one navigation bar for all tab views?
UPDATE
Basically my main question is should I have one navigation bar for all views or each tab should have its own navigation bar?
What is best practice in "iDevelopers" world.
I'd rather have the self.window.rootViewController be a navigationController, and afterwards, just push your tabBarController or
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// create someViewController
[viewControllers addObject:someView];
// create someViewController2
[viewControllers addObject:someView2];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tabController]
self.window.rootViewController = navController;
If you want to have navigation bars on each controller, you should to have UINavigationController on each tab of your UITabBarController
Look an answer to this question.
You you need navigation bar you have to add UINavigationController as you did for each view controller in tab bar. So self.tabBarController.viewControllers will have all navigation controllers.
Its not good idea to switch across tabs without user intervention.

Issue with pushing UIViewController on UITabbarController

I have implemented UINavigationController and UITabbarController with each other. I am able to see Navigation bar and Tab bar along with UIViewController.
Problem is when I push any other UIViewController on this controller,that viewcontroller get pushed but Tabbar get disappeared.
Is there any provision to persist that UITabBar along the stack????
below is code I am referring
UIViewController* cont1 = [UIViewController alloc]init];
UIViewController* cont2 = [UIViewController alloc]init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:cont1,cont2,nil]];
Thanks.
You should do it the other way around.
Place an UINavigationController in each of the UIViewController in the UITabbarController.
YourViewController *viewController = [YourViewController alloc] init] autorelease];
UINavigationController *controller = [UINavigationController alloc] initWithRootViewController:viewController] autorelease];
controller.title = #"something";
controller.tabBarItem.image = [UIImage imageNamed:#"xx"];
tabController.viewControllers = [NSArray arrayWithObject:controller];
then you can push sub view controllers into navigation controller with tab bar persistent at the bottom of the screen.

Creating a programmatic tab bar with storyboard view controllers?

I have a tab bar that is created programmatically and I'm having difficulties initializing a storyboard associated with a view.
I'm able to load the view successfully in the tab bar without the storyboard (see code below) but the view is only partially showing because some of the UI components are in the storyboard.
The name of my storyboard is MainStoryboard and I set the storyboard view identifier to SettingsViewController.
How can I initialize my storyboard for SettingsViewController in the code below?
- (void)createTabBarItems {
tabBarController = [[UITabBarController alloc] init];
settingsViewController = [[SettingsViewController alloc] init];
UINavigationController *sett = [[[UINavigationController alloc]
initWithRootViewController: settingsViewController] autorelease];
[sett.tabBarItem setTitle:#"Settings"];
[sett.tabBarItem setImage:[UIImage imageNamed:#"settings.png"]];
[tabBarController setViewControllers:
[NSArray arrayWithObjects:sett, sett, sett, sett, nil]];
}
If you want to initialize the view controller as in the storyboard you have to use the storyboard methods instead of allocating the view controller directly:
// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// either one of the two, depending on if your view controller is the initial one
settingsViewController = [storyboard instantiateInitialViewController];
settingsViewController = [storyboard instantiateViewControllerWithIdentifier:#"SettingsViewController"];
Swift 4
let storyboard = UIStoryboard(name: "Main", bundle: nil)
settingsViewController = storyboard.instantiateInitialViewController()
settingsViewController = storyboard.instantiateViewController(withIdentifier: "SettingsViewController")

Navigation-based app with TabBar

I'm using Navigation-based Application Template with Core Data. Could anyone please tell me how to and a TabBar on the bottom of the view. I am using UITableView, so If I add UITabBar as subview, the TabBar is moving along with tableView when scrolling. I would like to switch between views with TabBar, first "segment" of TabBar should open the RootView (NavigationBar with TableView),and second some other view.
Now I did this:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewController = [NSArray arrayWithObject:yourNavigationController];
self.window.rootViewController = tabBarController
[tabBarController release];
that works fine, but how can I add more Items to UITabBar and for each Item some other view? TabBar has now just one Item on which rootView is loaded
Thanks!
Use UITabBarController as your root view controller in your application delegate:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewController = [NSArray arrayWithObject:yourNavigationController];
self.window.rootViewController = tabBarController
[tabBarController release];
Its simple one, just add UITabbarController to your code and then made the first tab controller to be a navigation controller. And point that navigation controller to your controller which has table view that you want to show.
If you are doing it programmatically you can use this:
FirstViewController *first=[FirstViewController alloc]]init];
UINavigationController *nav=[UINavigationController alloc]]initwithRootViewcontroller:first];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewController = [NSArray arrayWithObject:first];
[tabBarController release];