I'm trying to come up with something simple, but i can't figure it out.
I have a UITabBarController and at one point I have to display another tab's UIViewController, and call a method in that new UIViewController, using data from the original UIViewController.
So basically I want to pass data to another UIViewController (that may not be initialized yet), and show the right tab.
If I use NSNotificationCenter, I'm not sure if the tab's UIViewController is initialized yet, and also it's a bit ugly to use delegation here.
What is a clean way to send and show data in the new tab?
A better idea would be to have a protocol implemented in the tab view controller the view controller can call to send data and set up any other view controller
You should have a data model of some sort (singleton?) that is accessible by both view controllers. When the second view controller is about to display its view, it should reference the data model to determine the data it should display.
int x = 1; //this is the view controller you want to go to.
MySecondViewController *secondViewController = (MySecondViewController *)self.tabBarController.viewControllers[x];
[secondViewController setDataObject:dataObject];
[self.tabBarController setSelectedIndex:x];
So what's happening is you're calling the controller from the tabBarController, it will initiate there if its not. Then you can set whatever you want on the controller, then you just switch to the selected tab.
Related
In my app i am using tabbar controller with 5 tabs,in 3 tabs,when click on a button it calls one View Controller. I am using same view controller for those 3 tabs so i am getting problem while calling same view in different tabs,So while changing the tab i dont want to call ViewWillAppear method. So what i have do? or else how to find previous selected index of the tabbar controller?
Thanks in Advance
I think you are not familiar with iOS Development.
In a TabbarController we can specify as much of UIViewcontroller/UINavigationController object.
if we need to re-use a UIViewcontroller you need to tag the view controller using a property object.
You need to use different instance of UIViewController for different tab.
While Showing a UIViewController its viewWillAppear got fired. we don't able to remove this behavior. But in most case we can handle such case with viewDidLoad (It is called once for an instance of view controller).
if we need to track the previously selected tab item we need to do our own work-around by using a shared class or static variable.
thanks,
Naveen Shan
I'm creating an app using the iPhone Utility App framework, and I'm trying to use a navigation controller on the flipside view, as there will be a lot of drilldown options on this view. When I'm done with this view, I call the following code:
- (IBAction)done:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
When I dismiss this view, I want to be able to go back to the place in the navigation I was currently at when I reopen this view again. However, when I dismiss this view using this method, the vc gets deallocated, therefore the menu starts back at the beginning when I try to go back to the menu.
Thoughts?
You'll need to retain a reference to the object (I'm calling it the options controller). I would say the easiest way is to create an iVar in the presenting view controller that references the options controller. Then, when you go to present the options controller again, just present the referenced options controller rather than creating a new controller. If different view controller objects can present the options controller, you'll need to either pass that reference around, or store it in some object that all the other view controllers have access to.
Hmm not much code so maybe I'm misunderstanding your setup, but...
You could use the AppDelegate to store (as a property) your current position (index) in the views collection of the Navigation controller, and then write a method that pushes to that (stored) position when you re-visit it later.
Might be an easier way to do it though..
So what you want is to flip from a view to another view? If you want to keep the navigation bar status between flipping, I recommend you use only one view controller to control this 2 views. you can use + transitionFromView:toView:duration:options:completion: of UIView to flip views.
I have 4 view controllers: VC1,VC2,VC3,VC4.
VC4 can be called from VC1,VC2,VC3, my point is how to get which one of view controller (VC1,VC2,VC3) has called VC4?
Flohei's answer is right, but a bit long winded given there's already a method to do this in iOS. There's no real need to add another property. You can use the parentViewController property to find out which view controller is currently displaying your modal controller.
You could add a UIViewController property to VC4 and set this one every time you create a VC4 instance to the current viewController.
If I want to replace one screen of an app with another, but I don't use a navbar/tabbar controller, then I could just remove oldViewController.view from window and add newViewController.view to it. That's all, now newViewController will get rotation events, etc.
But UIView does not reference "its" controller, so how is this possible, how iOS know it should make newViewController an active one? Does iOS do some magic, it internally references controller from view or what?
UPDATE:
I think I was misunderstood: I don't ask how to make some view controller an active one - I know that. I'm just curious, how is it possible that I pass some view to UIWindow object ([window addSubview:view]) and it somehow finds view controller although view doesn't know its controller.
yeh I had the same question like you. and I figured it out.
UIView is derived from UIResponder. and UIView must subclass UIResponder::nextResponder.
Its default implementation is returning a view controller of the view (if it hadn't, it would be super view)
So, consequently view can see its controller. that means window know the topmost view and also
its controller.
good luck.
Unfortunately, iOS only send events to the first ViewController of the stack. You can try and present a new one on the top of others with video for example, it will never rotate.
If you don't use navbar/tabbar controller you will have to add and remove everytime from the Window to keep only one at the time if you wand to have events.
The main UIWindow class for your application will have a view controller set in its rootViewController property. That controller's view is the "main" view for the app. This is usually setup in the main .xib for the project. That view controller will receive the usual events like "viewDidAppear" or "willRotateToInterfaceOrientation". You can put up your own view over top of it if you want to, but you will need to manage those events yourself. Usually you don't do that though. You just use a UINavigationController or UITabBarController as your rootViewController and allow them to manage getting the events to new "pushed" view controllers, or you popup view controllers with "presentModalViewController".
i want to know how to send string between two views of a tab bar controller?is it done the same way as with simple two views?
i have an application with tab bar controller with two tabs.there is a textfield in first view.i want to send the text to second view
There are many way to do that ! Here are two easy ways :
You can use the viewControllers property of your tabBarController.
You can use notifications to send the modification from one to other.
Ask me if you need more details !
Set the string on a model the two views can share, either as a singleton or a descendant of a singleton. Sounds like you just want to transfer data between two views and while it's possible to link them together, it's would be terrible style.
MVC, baby!
i make new method in new view, to access the string, so before u push the newController u should acces that method with String as parameter, follow this :
in my newController i write this :
#implementation newController
NSString *stringRef;
-(void) constructor : (NSString*) stringParameter{
stringRef = stringParameter
}
and on the before view controller i write this :
#import "newController.h"
#implementation viewController
UIViewController *new =[[newController alloc] init];
[new constructor:#"this string is sending to new controller"];
[self.navigationController pushViewController:new animated:YES];
[new release];
it should be works guys,, have a nice try. :)
If there is supposed to be a common element to two different view controllers within tab bar controller, you may want to include that common element as a member object of a custom tab bar controller. When view A disappears, it can access its parent view's member object and update it, and when view B appears, it can access the same object and update itself (and vice versa).
But on another thought, it sounds like you are doing something with a tabBarController that really should be done with a navigation controller. It isn't normal UI to have two different tab bar views that share data in that way. Users are not expecting changes made in one tab to affect what is displayed in another tab. When data in one view is dependent on data in another view, it would be more typical that the second view would be pushed onto a UINavigationController stack after the first view. Remember that it is possible for a UINavigationController to be one of your views on the UITabBarController.