Push another view controller into a UITabBarController view - objective-c

For the navigation in my app I'm using a UITabBarController. This works fine, but in one of my viewcontrollers I want to push another view controller into the tabbar view.
In other words I want to replace the selected viewcontroller with another one.
I'm doing this with the following code:
self.tabBarController.selectedViewController = self.otherViewController;
The list of viewControllers in my TabBarController does not contain the otherViewController.
This trick works fine in IOS 4.3, but IOS 5 does not like it.
Does anyone know a solution which is accepted by IOS 5?

You want to REPLACE that view controller in the tabbar with another view Controller?
If so, you have to edit the viewControllers property in the tabbar by setting a new one. It would be something like:
UIViewController *thisIsTheViewControllerIWantToSetNow;
int indexForViewControllerYouWantToReplace;
NSMutableArray *tabbarViewControllers = [self.tabbar.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:indexForViewControllerYouWantToReplace withObject:thisIsTheViewControllerIWantToSetNow];
self.tabbar.viewControllers = tabbarViewControllers;
[tabbarViewControllers release];

You can't just use a navigation controller or similar on this tab?
Anyway this should work:
NSMutableArray *controllers = [NSMutableArray arrayWithArray:rootTabBarController.viewControllers];
[controllers replaceObjectAtIndex:rootTabBarController.selectedIndex withObject: newController];
rootTabBarController.viewControllers = controllers;

Related

Detect rootcontroller in iphone sdk

I have using two types of controller in my application i.e. NavigationController and presentViewController.
How can I detect base controller at any instance through code i.e I am using navigation or presentviewcontroller to transist one viewcontroller to another viewcontroller?
Try this for take rootviewcontroller of navigation:
UIViewController *topViewController = [self.navigationController topViewController];
for present modal view controller check out the 'presentingViewController' property of UIViewController and for navigation you can get the array of view controllers NSArray *ArryViewControllers=[self.navigationController viewControllers]; and then get the object at index 0 . this will be the root view controller of that navigation controller.

How to TabBarController display differents Items?

On my firstViewController I have a tabbar that contains my firstViewController and a helpViewController.
When I click on a button from the FirstViewController, I push a NewViewController. But, when this view is pushed, I want to change the content from the TabBarController to display other ViewControllers, like infoViewController, optionViewController and NewViewController. Is that possible?
The First Image represents my application. The FirstViewController has a button that will push the NewViewController. When the user clicks this button, I want that my app shows what is in the second image. Is possible?
Yes, this is possible (I just did a proof of concept in Xcode). Assuming that you are using storyboarding, you need to make your initial view controller a UINavigationController otherwise you won't be able to use the push segues. Then, make the first UITabBarViewController the root view controller of the navigation controller. Put an entirely new UITabBarController into the storyboard, and then put a UIButton into the firstViewController and link it via a push segue to the new (second) UITabBarController.
When you tap the button the old tab bar will slide off, and the new one will slide on.
Here's an example of how it all looks:
!!This app uses navigationController and TabBarController!!
Using the storyboard I saw each piece of the app, then I had the Idea: Insted of pushing the NewViewController, how about push a tabBarController? When the user clicks the button, the app will push the tabBarController with 2 TabController`s.
Just add New File to your project, sub classed UITabBarController. Then add this code to the init method of your tabBarController: self.hidesBottonBarWhenPushed = YES;
On ViewDidLoad just alloc and init what views you want to display on the tabBar and
self setViewControllers:[NSArray arrayWithObjects: vc1, vc2, vc3, nil]];
Working fine here :D
You can nest TabBarControllers. But that would look strange. And the first TabBar wouldn't be changed. Pushing a TabBarController into a TabBarController is not possible because TabBarController does not support pushViewController. Thats only possible with a NavigationController.
Anyhow you can change the content of the TabBar completely programatically.

Converting iPhone application into iPad, and then use splitview in iPad?

I've made a diary application for iPhone and I want to make it universal (iPhone and iPad).
When the application Launches in iPad, I want it to use a split view controller.
I have two classes. The first one is "Rootviewcontroller" and the second one is "Detailview" Controller. In both classes, I use the navigation controller. In the iPhone, when the application launches, rootviewcontroller is visible. Using the navigation controller, the user can move to the detail View.
On the iPad, I want the root view controller to be on the left side of the split view controller, and the detail view on the right side.
If you check out the Apple Documentation, you just have to assign the two view controller when you initialize the UISplitViewController. Here's a link to the Apple Documentation - http://developer.apple.com/library/ios/#documentation/uikit/reference/UISplitViewController_class/Reference/Reference.html
Here's an example from an actual iOS application we have (changed some variable names to make it easy to understand). We basically determine if the device is an iPad or not, then build the master navigation controller.
detailNav is the a navigation controller created with the "detail view controller of our item"
masterNav is the navigation controller used with our iPhones. It starts the users on a tableView which allows them to select an item to move forward to a detail view.
We assign both of these to an array and initialize the split view controller.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detailVC];
NSArray *vcs = [NSArray arrayWithObjects:masterNav, detailNav, nil];
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
[splitViewController detailVC];
[splitViewController setViewControllers:vcs];
[[self window] setRootViewController:splitViewController];
} else {
[[self window] setRootViewController:masterNav];
}
This is most likely not the best code or best practice as me and my team are still fairly new to the iOS world, but I hope it helps. This code is running on a live app in production.
Here's apples docs on how to do it: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html
To implement the split view it is in the iPad view controller, if you want to change the side the split view is on you can subclass it and redraw it on the right. Hope this helps!

How to 'pop' to a view contoller not on the stack

I have a navigation controller (XCode4 with Storyboards and ARC) where a segue is connected to each individual ViewController (7 of them). Normally, I tap a row in the Nav contoller which takes me to the correct scene. However, there are times that I want to go from scene "A" to scene "C" using segues, then from "C" to "B", which has NOT been placed on the stack by going through the Nav Controller.
Is this somehow possible (to go from scene "C" to scene "B")?
UPDATE: this is the code to put controller on the stack:
EnterDataViewController *edvc = [[EnterDataViewController alloc]init];
NSMutableArray *ma = self.navigationController.viewControllers;
[ma insertObject:edvc atIndex:1];
self.navigationController.viewControllers = ma;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex: 1] animated:YES];
Insert the new view controller below the top one in the hierarchy, with animation off. Then, pop. :)
The stack is an array stored in the UINavigationController's viewControllers property. Use it to create a new (mutable) array where you could just insertObject:atIndex:1 and assign this back to the UINavigationController.viewControllers.
Inserting new controllers to the self.navigationController.viewControllers doesn't always work as expected, so I'd recommend to fetch controllers that you need from the array, build a new array from scratch, and then assign this array to navigation controller.
UIViewController *mainScreenController = [[self.navigationController viewControllers]objectAtIndex:0];
SecondScreenController *secondScreenController = [[SecondScreenController alloc]init];
NSMutableArray *controllers = [[NSMutableArray alloc]initWithObjects:mainScreenController, secondScreenController, nil];
[self.navigationController setViewControllers:controllers animated:YES];

how to add a splitview controller in a tabbar

i m having a problem where there is a need to add a split view controller in my tabbar applicaion .i need to add a split view controller in my second tabbar item .is der any possibility to add a splitivew controller in my second tabbar .
To add a splitViewController programmatically you use something like this.
UISplitViewController *c = [[[UISplitViewController alloc] init] autorelease];
c.viewControllers = [NSArray arrayWithObjects:myVC1, myVC2, nil];
And to hide those navigationbars of those myVC1 and myVC2 you will have to use
myVC1.navigationController.navigationBarHidden = YES;
myVC2.navigationController.navigationBarHidden = YES;
Hope it helps ;)