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.
Related
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.
So I pretty much built my first app(single view) but i've now decided I should add a little "about/info" button and just give a tiny amount of information on how to use the app and a website to go to.
I wasn't sure about the best way to go about this but storyboards seemed really convenient. A user here answered my question about adding a storyboard to my existing project which seems to work.
Now one question is can I use my current viewcontroller h and m files for the second view (the about screen). I suppose it would technically work if I set the Viewcontroller to the same one as my primary app view.
the only thing this second view is going to have is text and a button to go back. Is it OK to set the connections in the storyboard and just let them use the same Viewcontroller files or is this a big no-no?
Thanks
It is recommended you use a different view controller for each view. In case of a static view controller in which the user doesn't interact with the view except maybe for navigation (which can be handled in the storyboard in many cases), I would just leave that view controller as a generic UIViewController, not a subclass.
Yes you can use the same view controller to control more than one view, however, from what I understand, that doesn't seem like good practice in your case
For instance, if you had similar views with almost identical outlets (say the views have the same UI widgets such as buttons, titles but their layouts vary significantly) you co[uld create a separate view for each different layout and use a single view controller. But from what I understand, in your case, those two views will have different outlets (buttons, labels, etc.)
For this reason, I'd suggest creating a new view controller. Then when you want to show this second view, you will have to present it from your active view controller. I'd highly recommend you read tutorials on view controllers in Apple developer resources. But very quickly, I'd suggest the following
UIViewController *vc2 = [[UIViewController alloc] initWithNibName:#"View2" bundle:[NSBundle mainBundle]]
[self presentViewController:vc2 animated:YES completion:nil];
And to go back to your main view, you'll have to dismiss this second view controller
[self dismissViewControllerAnimated:YES completion:nil];
Of course, they don't have to be animated, you can set them to NO.
Hi I have placed three button similar to UISegmentcontrol. But its not, its just image buttons. Here I need to have a class called MainView(UIView) and child views like Breakfast, Lunch and Dinners(UITableView for all child view). By default MainView should load the breakfast class if i click the dinner button it should take me to the dinner class. I am able to work with single tableview but I dont know how to load other classes on clicking the button from the parent class.
The child views should appear for the full screen except the navigation items. Can you please provide me you support? Thanks
Switching between multiple different views using a UISegmentedControl, similar to iCal or the AppStore application.
Follow this link: HERE
The solution in this link is hands down the best solution I've found about the issue so far. With a little bit of adjustment it also worked fine with a tabBar at the bottom.
You want to display a new view controller on your navigation controller.
// .h file
- (IBAction)dinnerButtonPressed;
// .m file
- (IBAction)dinnerButtonPressed {
DinnerViewController *controller = [[[DinnerViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
}
And connect your button to the dinnerButtonPressed action.
The method creates a new DinnerViewController and displays it onto your navigation controller. You can then configure the layout of the dinner view controller in it's own xib (for example, you could add a table view ...)
From your comments to deanWombourne, I understand what you want to do. To accomplish that you have to modify the datasource that your current tableView is linked to, and then reload the tableView with new datasource.
But if I were you, I would go with deanWombourne recommendation and put each of those breakfast, lunch and dinner in separate views and push them into the stack, or I would go with UITabBar and put a tableView in each.
I'm not going to say this is good design by any means. I'm kind of inheriting something that is existing. Anyway, there is a TabController. One this one tab, there are two views that get loaded on demand based on a UISegmentController. Both of these two ViewControllers are subclasses of another ViewController that has methods I need.
When I'm in the TabController, I want to create a method that uses some of the superclass methods of the two ViewControllers. How do I get access to the tab's current ViewController since it's loaded on demand? Do I need to have a reference of the base controller type, and just have it set to the current view controller when it gets loaded on demand? Thanks.
To get the active tab showing on the screen, you could use [self.tabBarController selectedViewController] which will give you a UIViewController Reference. If you want to use the methods then you can cast it to your ViewController Superclass and then call methods on it like so (where self.tabBarController is your Tab Bar Controller):
MySuperClassViewController *viewC = (MySuperClassViewController *)[self.tabBarController selectedViewController];
[viewC someMethodDeclaredHere];
First, a little bit of terminology to clear up some potential confusion: I assume you mean a UITabBarController which controls various view controllers. Also, it seems you are using a UISegmentedControl which is not a view controller but a subclass of UIView. I hope these are just typos and not conceptional problems.
There is still some ambiguity about what you mean with "when I am in the tab controller". I assume you want to put code into the class representing the UITabBarController. But why? Just put these methods into the appropriate view controller, or if it is something that has to be done before, into your app delegate. However, if these methods are in a view controller superclass, they should have something to do with the logic necessary for this view controller.
If you need the methods elsewhere, i.e. outside your view controllers, consider creating a separate #include file and putting the methods there. Alternatively, you can use your app delegate which is conveniently accessible through [[UIApplication sharedApplication] delegate].
I have a UIViewController — let's call it "FormController" — which is simply a form that edits an object. I want to use it in 2 different situations:
Creating a new object — using using UINavigationController's presentModalViewController: method.
Editing an existing object — push the view controller onto the UINavigationController stack, not using a dialog method.
There is a slight difference in that in the modal situation I would like to have a toolbar with "Cancel" and "Done" buttons, whereas in the stack situation I would like to just have the navigation bar provided by the UINavigationController.
This would be similar to the Contacts application where the "New Contact" and the "Edit Contact" screens seem to use the same view controller, but the New Contact form is presented modally while the Edit screen is pushed onto the navigation stack.
My question is: What is the best way to handle both situations without having to write 2 separate, but mostly identical view controllers?
I thought about creating a "ModalFormController" which encapsulates the bare "FormController" through composition and adds a toolbar, but I read somewhere in the docs that Apple doesn't recommend nesting view controllers.
Why not use subclassing? Make ModalCreateFormController a subclass of EditFormController and handle the modal-specific stuff in the subclass.
What I do (sometimes) is set up an enum that specifies the type of the view controller.
For example, you might have two types: an Edit type, and an Add ("new") type.
The Add type is implemented via a modal view controller, while the Edit type is pushed onto an existing navigation stack.
In the view controller's -viewDidLoad: method, I simply do a switch/case tree that sets up the title and other appearance characteristics depending on the type enumeration specified above.
The nice thing about this is that it is easy to add a new type. The downside is that the conditional tree for handing this enumeration can get complicated quickly, depending on how different the types are.
But the switch/case tree makes it a lot easier to manage.
So, it depends on what you're trying to do with the two types. But it's definitely doable.
In addition to having an explicit property on the view controller (as Alex Reynolds suggests), two other approaches that occur to me are:
If you have some kind of model object that you're editing, ask it for its current state. If it's ever been saved, then you're in edit mode. Otherwise, you're in create mode.
Look at the value of the controller's parentViewController property. If it's an instance of UINavigationController, then you're in the navigation stack. If you're being displayed modally, it'll be an instance of your list controller.
Ug, I hate extra ivars…
I use this instead:
if([[self.navigationController viewControllers] objectAtIndex:0] == self){
//Modal
}else{
//Pushed
}
It is a bit of a hack, but we are using the logic that if the offending view controller is the first in the stack, you can't go back. Actually we are ignoring the fact of whether it is modally displayed at all.
I had to do this a bunch of times in my app and after trying a couple different ways of doing it, including modal subclasses & a re-usable modal helper classes that used forwardInvocation. I found the best pattern was to make a containingModalViewController method each view controllers that (usually) creates and returns a UINavigationController for the caller to use with presentModalViewController.
In most cases this method builds and returns a UINavigationController with self as the root view controller (with repeated calls to the method checking self.navigationController and returning that instead if it's not nil). Other cases I made a dummy root controller first and pushed self on second in order to get a back button. Then a trick can be used to catch the back button press: http://smallduck.wordpress.com/2010/10/05/intercepting-uinavigationcontroller/
In some cases the view doesn't need a navigation bar and so this method just adjusts some flags and returns self. I even found in some cases that did need a navigation bar it was simpler to make that method invoke self.view, then tweak the view hierarchy to add a UINavigationBar and again return self. But in any case, the setup often isolated to that one method, and the caller handles it the same in each case.
Apple explains how the contacts application works under the hood:
To allow a custom view controller class to be used to both display and edit content, override the setEditing:animated: method.
You get some functionality for free, e.g. Edit/Done button.