Set Navigation Controller Parameter from TabBarController - ios

I've been struggling with this for quite some time and I can't seem to find anything here that directly relates. My goal is to pass a core data object from the login screen to my first view. My first view is linked to a navigation controller and the navigation controller is the first tab on a tab bar controller.
Through prepareForSegue, I can pass the object no problem to the tab bar controller but that's when things get confusing, how do I pass the object stored in the tab bar controller to the navigation controller?
Any help would be much much appreciated!

The TabBar is holding the array of ViewController and since your ViewController is inside NavigationController you can use this code to pass the object you want to the ViewController inside the NavigationControllerfrom prepareForSegue
UITabBarController *tabBarCtr = segue.destinationViewController;
UINavigationController *navigationController = [[tabBarCtr viewControllers] objectAtIndex:0];
YourViewControllerClass *viewControl = [[navigationController viewControllers] objectAtIndex:0];
viewControl.myObject = self.myObject

This works too:
UITabBarController *tabBarCtr = segue.destinationViewController;
SearchItemTypeViewController *viewControl = [[tabBarCtr viewControllers] objectAtIndex:0];

Related

Add a second UINavigationController

I have my first NavigationController set up with root viewcontroller - PhotoViewController, It's a collectionView with grid of images. When image is selected I wan't to present a second navigationController with root controller - DetailViewControler.
My first navigationController is set up in appDelegate. I really don't understand how and where should I create the second. I'm not using IB. At the moment I have this in my DetailViewcontroller viewDidLoad:
DetailViewController *detailView = [[DetailViewController alloc] init];
self.detailController = [[UINavigationController alloc] initWithRootViewController:detailView];
But when i'm trying to push a new controller, nothing happens. I guess that my code is in wrong place.
When I try to present second controller modally from my PhotoViewController, I have an error(trying to present nil viewController).
The common idea is to have a single navigation VC that contains -- and lets you navigate between -- other VCs. In the situation you describe, you wouldn't create another navigation VC. Instead, create just the detail VC and push that onto the existing navigation VC...
// assuming self is a view controller contained by a navigation controller
self.detailController = [[DetailViewController alloc] init];
[self.navigationController pushViewController:self.detailController animated:YES];
Use a parent view controller as your initial view controller and add the navigation controllers as children, then transition between them. See this blogpost that I wrote: http://sketchytech.blogspot.co.uk/2012/09/container-view-controllers-parent-view.html

showing one viewController from another viewController, when both are part of a UITabBarController

I have a UITabBarController that contains a UINavigationController each as TabbarItems.
Each UINavigationController has it's own UIViewController as the rootViewController.
Now, one of the rootViewControllers processes some information and sends it to another rootViewController.
Then, this rootViewController further processes that info and displays it on it's UI.
Till now, i have done this:
//from the current VC, taken the reference of the viewController
//to which i have to pass the data.
UINavigationController *controller = (UINavigationController *)self.parentViewController;
UITabBarController *cont = (UITabBarController *)controller.parentViewController;
//called the method of the target viewController that will do further processing
//(to which i will pass the data).
CustomerCareViewController *customerCare = (CustomerCareViewController *)[[cont viewControllers] objectAtIndex:0];
[customerCare setSRNumber:SRNum];
Now, I need to display the processed info (that will be shown on the UI of the target controller)
If you just want to switch from one tab to the other, use
self.tabBarController.selectedIndex = newIndex;
try this
[objAppDel.tabBarController setSelectedViewController:[[objAppDel.tabBarController viewControllers] objectAtIndex:1]];
hope this will help you.

General way to pass data to UIViewControllers when using a UITabController

I still haven't grasped this transfer with the structure below. I have read many posts, and have seen the same unanswered post by others, but no resolution.
I will try to simplify the question to make it easier for all.
The structure of the project is:
UITabbar with tab1 and tab2
Tab1 has a Nav controller-->ViewController1
Tab2 has a Nav controller -->ViewController2
In viewcontroller1 (tab1) I have object X.
In ViewCOntroller2 (tab2) I want to display object X.
Don't worry about displaying, that's the easy part.
Question: How do you pass object X from tab1 to tab2. (what is the general pattern).
If you want to do it using prepareForSegue, is this ok, or is there a better way.
If using prepareForSegue, where do you drag the segue to?
The tabbarcontroller
OR*****
2. to the second VC
Hopefully this is clear enough. With this in mind how would you perform the transfer?
Using the segue 1:
I tried doing this:
//(From View controller 1)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"CreateObjectXToDisplayObjectX"])
ViewController2* vc2 = [[ViewController2 alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc2 = (ViewController2 *)[[tbc customizableViewControllers] objectAtIndex:1];
//Crash here with with [MainNavigationControllerDesign setViewController1Delegate:]: unrecognized selector sent to instance 0x1064ef70'
vc2.viewController1Delegate=self;
vc2.objectXAtViewController2 = _objectXFromViewController1;
}
}
So, how is this Object X transfer accomplished?
Thank you in advance
You don't want to use segues in this way. Segues always instantiate new controllers when you go to them, but you already have these controllers embedded in the tab bar controller. If you were setting this up in code, I would say use a delegate, but if you set this up in IB, it's hard to do that. From VC2, you can get a reference to VC1's navigation controller with self.tabBarController.viewControllers[0]. VC1 will be that navigation controller's topViewController, so, putting that together, and adding a cast, you can access VC1 like this:
ViewController1 *vc1 = (ViewController1 *)[self.tabBarController.viewControllers[0] topViewController];
Once you have that reference, you can access any of vc1's properties. Don't forget to import ViewController1.h into ViewController2's .m file.

transition from viewcontroller to tab bar controller with a navigation controller at each tab error

Hi I added a prepare for segue code in order to transfer information, however I'm getting an error.
I have a tab bar controller. It has 4 tabs. Each tab has a navigation controller, and a VC as a root view controller.
from tab 1-->nav controller-->VC 1 I need to take a value to tab2-->nav controller --->VC1
(also is the segue connected to the nav controller at tab 2, or the root view at tab 2)
Thank you in advance
error:customizableViewControllers]: unrecognized selector sent to instance 0xf19bd60
(but I am going to tab 2 (index 1))?
where is my error?
if ([segue.identifier isEqualToString:#"ResultsSegue"])
{
//the root VC at tab 2
ResultsIndexViewController*controller=[[ResultsIndexViewController alloc]init];
UITabBarController *tabController = [segue destinationViewController];
controller = (ResultsIndexViewController*)[[tabController customizableViewControllers]objectAtIndex:1];//crashing here
controller.resultsArray=[NSMutableArray arrayWithArray:_matchedResultsArray];
}
}
This part of your code
[1] UITabBarController *tabController = [segue destinationViewController];
[2] controller = (ResultsIndexViewController*)[[tabController customizableViewControllers]objectAtIndex:1];//crashing here
Is full of misconceptions.
In [1] you are assigning a destinationViewController (type UIViewController) to an object of type UITabBarController. This is compiler-legal (UITabBarController inherits from UIViewController so they are both of type UIViewController) but it doesn't reflect really what is going on. Your segue will be to the UIViewController or it's UINavigationController.
So in line [2] when you try to use your tabController as a tab bar controller, it crashes (because it isn't one).
You need to redesign your project... forget the Segue, and use the tab bar controller. Something like...
- (IBAction)moveToTab2:(id)sender {
ResultsViewController* resultsVC = (ResultsViewController*)
[[self.tabBarController viewControllers] objectAtIndex:1];
resultsVC.resultsArray=
[NSMutableArray arrayWithArray:_matchedResultsArray];
[self.tabBarController setSelectedViewController:resultsVC;
}

Storyboards: Push a tableview into a tabbar view

I'm new to storyboards and I'm trying to achieve this without much progress.
I have a table view controller with cells that currently push into a detail view controller.
What I want to do is to create a tab bar but only for this detail view controller (which will have 3 views that can be navigated using this tabbar).
I can't find the way to achieve this.
Ok I found out that I was setting a property using [segue destinationViewController] but with the new tabbarcontroller in the middle of the flow now I have to do this:
UITabBarController *tabBarViewController = (UITabBarController *) [segue destinationViewController];
HotelDetailViewController *hotelController = [[tabBarViewController viewControllers] objectAtIndex:0];
hotelController.hotel = hotel;

Resources