I have iphone.storyboard and ipad.storyboard.
iPhone storyboard has UITabBarController as root view controller . Now i have to start with iPad storyboard. I need a split view controller here and apple says it has to be the root view. now i am adding a split view controller and then UITabBarController to iPad storyboard. My app is crashing due to the code -
UITabBarController *tabBarController =
(UITabBarController *)self.window.rootViewController;
UITabBarItem *tabBarItem0 = [tabBarController.tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem1 = [tabBarController.tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem2 = [tabBarController.tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem3 = [tabBarController.tabBar.items objectAtIndex:3];
When i add a simply UITabBarController with four tabs in iPad story board it works fine.
Shall i add any check for the device.. how to solve this issue as i have to use split view controller?
pls help
You should split code for iPhone specific and iPad specific like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UISplitViewController *splitViewControlelr =
(UISplitViewController *)self.window.rootViewController;
// work with your split view controller
}
else
{
UITabBarController *tabBarController =
(UITabBarController *)self.window.rootViewController;
// work with your tab bar controller
}
Related
I have this..
How do I add another tab screen using iOS5 storyboard, so that "Account" can have 3 screens.
Account ---> Account Listing ---> Account Details
You first drop in a new ViewController, and then you Ctrl-drag from the Tab Bar Controller to that new controller.
This brings up a popup where you can select "Add Relationship Segue". This connects it as a third tab.
This can be done programmatically:
// Tab Controller
UITabBarController *tabBarController = [[UITabBarController alloc]init];
// Views to be accessed
UIViewController *controllerOne = [[UIViewController alloc]init];
UIViewController *controllerTwo = [[UIViewController alloc]init];
UIViewController *controllerThree = [[UIViewController alloc]init];
// Store UIViewControllers in array
NSArray* screenControllers = [NSArray arrayWithObjects:controllerOne, controllerTwo, controllerThree, nil];
// Add Views to Controller
tabBarController.viewControllers = screenControllers;
Or using InterfaceBuilder:
Adding 'Tab Bar Items' to the hierarchy of views in the left-hand panel
Or using Storyboard:
iOS Storyboards (Scroll down/Search for 'Just Add It To My Tab')
New to iOS development, I've been following the tutorials on developer.apple.com, and am now adding functionality to those examples to further my knowledge.
The "second ios app" tutorial gives you a navigation controller based app. Extending this app, I want to have a tab bar controller as the first view controller.
So I now have the following setup:
All good. But there is code in the BirdsAppDelegate (a UIApplicationDelegate) which was relying on the navigation controller being the root view controller, so it can create and assign the "datacontroller" object.
This is the original code (before I added the tab bar controller):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
BirdsMasterViewController *firstViewController = (BirdsMasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init];
firstViewController.dataController = aDataController;
return YES;
}
Now this code fails because it assumes the root view controller is the navigation controller.
I have updated the code so that it works - but in my opinion it is ugly, and would have to be changed every time I make a change to the view controller hierarchy:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = (UINavigationController *) [[tabBarController viewControllers] objectAtIndex:0];
BirdsMasterViewController *firstViewController = (BirdsMasterViewController*) [[navigationController viewControllers] objectAtIndex:0];
BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init];
firstViewController.dataController = aDataController;
return YES;
}
So my question is: What is the better way to do what I am doing in the code above, so that any changes to the hierarchy will not break the code?
How do I programmatically access the view controller I am after in the application delegate, so that I can create and assign it's BirdSightingDataController object?
Thanks!
You can loop the [navigationController viewControllers] array looking for an instance of BirdsMasterViewController... Using [obj isKindOfClass:[BirdsMasterViewController class]].
You don't even need that code at a all. If you just want to change the controller, go to the storyboard and select the viewController you want to change to a TabBarController. In the Editor menu, there is an option for "Embed In", the choices are TabBar and Navigation controllers.
I always start with a single view application template. There is no code in the "application didFinishLaunchingWithOptions:" method,(except to return YES). You can set any viewController as your initial view in the storyboard, by setting the is initial View Controller check box, or just dragging the arrow to the viewController you want as your initial view.
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;
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];
I used a TabBar in my iPhone application and integrated it programmatically.
I have several TabBarItems in it and every click will load a XYZ-ViewController with a corresponding XYZ-View.xib.
Now I use to have a screen with the TabBar and a NavBar with SearchBar and a segmented control like in this example of Apple:
http://developer.apple.com/iphone/library/samplecode/TableSearch/index.html
But in this example, there is not a TabBar and copy the source into my project causes the problem, that I did the loading in XViewController.m viewDidLoad method:
// Add create and configure the navigation controller.
MyAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
// Add create and configure the navigation controller.
UINavigationController * navigationController = [[UINavigationControlleralloc] initWithRootViewController:self];
myAppDelegate.navController = navigationController;
[navigationController release];
[myAppDelegate.window addSubview:myAppDelegate.navController.view];
The effect is, that there is a toolbar with the correct title, but there is no searchbar and no TabBar at the bottom.
Could anyone give me a hint what's wrong here?
I solved the problem:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:test2ViewController];
NSArray* controllers = [NSArray arrayWithObjects:test1ViewController, navigationController, test3ViewController, nil];
[self.myTabBarController setViewControllers:controllers];