How to make an method change the root view - iphone

My app is structured like this:
- Window
- Navigation controller
- Table view
- Button (in one of the rows)
- Tab bar controller
- tab..
- tab..
- tab..
(The tab bar controller isn't added as a sub view so it can't be seen)
How would you make the button able to manipulate the Navigation controller and tab bar controller objects?

Well you could have them as properties in your AppDelegate like this:
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain) UITabBarController *tabBarController;
And than in your table view you can get a pointer to them like this:
UINavigationController *navigationController = [(YourAppDelegateClass *)[[UIApplication sharedApplication] delegate] navigationController];
UITabBarController *tabBarController = [(YourAppDelegateClass *)[[UIApplication sharedApplication] delegate] tabBarController];
After this you can have your button do whatever you want to them. Let me know if this works for you.

Your description is not accurate. Both UITabbarController and UINavigationController are controllers of multiple view controllers. So, in your structure, you should add a UITableViewController between "Navigation controller" and "Table view".
According to Apple's View Controller Programming Guide for iOS, you can assign both view controllers or tab bar controllers to the items to be controlled by a tab bar controller.
Thus, you could have this structure:
- Window
- Tabbar Controller
-Navigation Controller
- Table View Controller
- Table view
- Button
- tab (another View Controller)
- tab
- etc.
You can hide the UITabbar when the table view is visible.
You could then access everything the way you would expect: from the UITableViewController it would work with self.navigationController, or self.tabbarController.

Given this description of what you would like to do:
The navigation controller is showing at the moment (its a login screen). I'd like to hide it and add the Tab bar controller as a subview
I think that you do not really need that the button has got references to the navigation controller and to the tab bar.
You can define a method in your app delegate that you can access directly from the button, like this:
[[(MyAppDelegate*)[[UIApplication sharedApplication].delegate] doSwapNavAndTabBar];
the app delegate is the most natural point to do this, since it is in a way "responsible" for the window object where you want to do the swap.
If the tab bar that you want to show depends in some way from the button, then I would suggest to pass the button reference as an argument to that method:
[[(MyAppDelegate*)[[UIApplication sharedApplication].delegate] doSwapNavAndTabBar:button];
as it is usually done to manage events generated through controls. The app delegate can then ask the button for the information necessary to decide on which tab bar to show or how to initialize it.

Related

Pushing Tabbarcontroller from a UIViewController

i`m wondering if its possible to push a TabbarController from UIViewController ?
what i want to do is:
when the user opens the app it shows a View with two buttons to select the language he want, this view does not contain anything except the two buttons, no tab bar, no navigation bar.
after selecting the preferred language it should push a tab bar view with 5 tabs in it. each tab contain a tableview controller.
is it possible ? if yes please explain how to do it i`m little bit new for this :)
thank you in advance :)
Well you can add it as subview to UIWindow of your app delegate. You have to make a property UIWindow of your AppDelegate class.
Now When in your UIViewController button is pressed, do something like this:
- (void) buttonPressed:(id)sender{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
UITabBarController *tab = [UITabBarController youWayOfInitializingIt];
[delegate.window addSubview: tab.view];
}
Sure, just present an UITabBarController from the UIViewController using presentModalViewController.
I would try dragging out two different tab bar view controllers, one for each of your languages. Ctrl-drag each button on your UIViewController to its respective language's tab bar view controller and choose the type of transition you want. However, if you want to use the push transition I suggest embedding the original UIViewController in a navigation controller. You can do this by clicking on the UIViewController and then...
Editor --> Embed in ---> Navigation Controller
This will provide you with a navigation bar and a back button if the user wants to go back and change the language.

Any "non TabBar-template" based tutorial on how to add a UITabBar?

I would like to add a TabBar to an existing view-based application I already started just to allow the user to switch to other parts of the app like the "About" section and another section entitled "Saved Searches" to display a navigational content (saved searches list > specific search result > product details).
Any idea on how to do this ? All tutorials I found point me directly to a TabBar template.
Thx for helping,
Stephane
You could start off with the UITabBar Application Template and you'll realize it's very easy to do:
In your UIApplicationDelegate class, in the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Instantiate a UITabBarController like this:
UITabBarController *tabBar = [[UITabBarController alloc] init];
Then you set the view Controllers that will appear on the tab bar:
tabBar.viewControllers = viewControllers;
Which is a NSArray you can previously create with your UIViewController subclasses:
NSArray *viewControllers = [[[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil] autorelease];
After this, you only have to set it as the root view controller of the window, or add it as a subview (it has the same effect, but the first approach doesnt work prior to iOS 4)
self.window.rootViewController = tabBar;
or
[self.window addSubView:tabBar.view];
And then
[tabBar release];
To achieve the kind of navigation that you say in your question, the view controllers you set to the tabBar should be instances of UINavigationController, which are very easy to create like this:
UINavigationController *vc1 = [[UINavigationController alloc] initWithRootViewController:firstViewControllerPage];
And inside them, you can push (navigate to) other view controllers doing:
[self.navigationController pushNavigationController:anotherViewController animated:YES];
Hope this brief review of it makes it a bit clear :)
You can create a new UITabBarController, and add it's view as a subview of your applications window. Then, add your other view controllers (for your "About" and "Saved Searches" sections) to that tab bar controller.
This can be done most easily in Interface Builder. In your MainWindow.xib, drag a Tab Bar Controller object onto the canvas. This will automatically create a tab bar with two items (one for each of the view controllers added). For each view controller under the tab bar controller, go to the identity inspector and change its class to your custom view controller subclass. Then, show the attributes inspector and there is a field "NIB Name" - again, set this to the appropriate nib name. Your custom controller views will then be loaded from their corresponding nib files. All that's left to do is name each tab in Interface Builder, and give it a graphic.
You can also do this programmatically if you don't like IB, by assigning the custom view controllers to the tab controller's viewControllers property, and assign a selectedViewController.
Hope this helps.
EDIT
Thought it might be helpful to show a little hierarchy! Your MainWindox.xib structure might look something like this:
AppDelegate
UIWindow
UITabBarController
UITabBar
AboutViewController (view loaded from "AboutViewController.xib")
Tab Bar Item - About
UINavigationController
Navigation Bar
SavedSearchesViewController - Root View Controller (view loaded from "SavedSearchesViewController.xib"
Tab Bar Item
And push appropriate view controllers from SavedSearchesViewController as normal to provide navigation content.

Converting an Utility application (2 view controllers) to a Tab bar Application

As the title suggests, I have built an utility based app.
The app consists (at the moment) of 2 view controllers + a model class hierarchy.
The thing is , as I'd like to add some features to the app , I would like to convert it to a TabBar based application.
As a first step , I would like the first view to be the first view of the tab bar , and the flipSideView to be one of the other tab bar items.
Is there any "standard procedure" / "grocery list" for such tasks ?
I'm sure some of you have encountered the same problem , and would love some advice on "slicing up" the app , and "wiring it up" after creating a new nib file for the main window (is that the first step ? )
Thanks in advance.
I would probably start as follows (assuming you are using Interface Builder and using the standard Xcode utility app template):
Edit your MainWindow.xib file and drag a Tab Bar Controller object into the top level of your view hierarchy.
The default tab bar controller in IB includes two view controller items as examples. Click the first one and change its class to be your existing Main View Controller class.
You can also set the icon and tab bar title for the main view controller item by adding the associated Tab Bar Item that IB has already added to the view hierarchy.
Do the same for the second tab bar item setting the class for the FlipsideViewController.
Delete the old version of your Main View Controller from the NIB file and also remove the IBOutlet property from the Application Delegate (you probably also have references to the main view controller in dealloc which should be removed).
In your App Delegate add an IBOutlet property for the Tab Bar Controller as follows (don't forget to synthesise the property):
#property (nonatomic,retain) IBOutlet UITabBarController *tabBarController;
In IB wire up the tabBarController outlet from the App delegate object to the Tab Bar Controller object.
Finally to get the tab bar controller to show up in place of the main view controller change the code in application:didFinishLaunchingWithOptions: to the following:
[self.window addSubview:self.tabBarController.view];
This should get the basic tab bar app up and running and you can add additional view controllers to the tab bar.
Since you no longer want to flip between the MainView and FlipsideView you can remove the references to the FlipsideViewControllerDelegate from the MainViewController along with the info button and its IBAction method showInfo. Likewise in the FlipeSideViewController you should remove the done button from the view and its IBAction method as these no longer make sense when used with the tab bar.
so, step by step :):
1. declare UITabController outlet in your app delegate.
2. in MainWindow.xib: first drag the instance of UITabBarController in document window, then make connection from app delegate to that instance. now you can set MainViewController as UiTabBarController's first tab viewcontroller, then set FlipsideViewController as UiTabBarController's second tab viewcontroller.
3. in app delegate's appDidFinishLaunching replace [self.window addSubview:mainViewController.view]; with [self.window addSubview:theNameOfUITabBarControllerOutlet.view];. that will do the work. sorry, I don't know your background, that's why I'm not very specific about performing a particular action I decribe, so let me know if you find yourself stuck

uiTabBar - use for menu NOT view control? (iPhone)

quick question - I have my "first view" which is going to be the ONLY view in my application. I've added a UITabBar to this view using Interface Builder. I am simply wanting to use this as a menu to control the contents of a scroll view.
For example, the user clicks on the first icon in the UITabBar - I get its tag, then based on that, will add a subview to the scrollview. This is working ok....
...but, I have been viewing a few tutorials on tabbars and it seems that 99% of the time they are used to control views. I simply want it to return my tags.
So my question is this: is what I am doing ok?? Can it be used for simply returning a value rather than changing a view? If this is common/OK practice, how on earth do I reference it?
I can get the selected item tag, but cannot actually reference the uiTabBar to make the first button selected. In my .h file, I tried to specify an IBOutlet for the controller, but I cannot link this in IB.
thanks for any info!
To receive notifications that a tab bar item has been clicked you need to modify your view controller to implement the UITabBarDelegate protocol and add an outlet for the tab bar. To do this, modify your declaration in MyViewController.h to something like this:
#interface MyViewController : UIViewController <UITabBarDelegate> {
UITabBar *tabBar;
...
}
#property (nonatomic, assign) IBOutlet UITabBar *tabBar;
Then implement the tabBar:didSelectItem method in MyViewController.m as follows:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"Tab clicked: %d", item.tag);
}
You must also set your view controller as the delegate of the tab bar in IB. (hint: connect up the 'delegate' outlet from the tab bar to File's Owner).
To access the tab bar from your view controller use the tabBar property and do things like:
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
As to whether this is a good idea - why not? All the tutorials show a tab bar being used with a UITabBarContoller to switch views, but it is designed to operate as a stand-alone control as well. As long as you are not breaking any HIG rules then how you implement your interface switching is up to you.

Adding UITabBar

I have a UINavigationBar based application - only a one navigation bar that is handling all the work with views in my app.
And I want to have a tab bar at a bottom part of every view that will take the user to whatever view he would like to go.
What is the easiest way to add a tab bar to an application?
Thank you in advance,
Ilya.
If you want to have a tab bar that is the same for all your views and is always visible, then you have to:
In your application delegate class, add an IBOutlet for the new UITabBarController named e.g. tabBarController
In your mainwindow.xib file, you have to add a tab bar view controller, interface builder will automatically create the tab bar for you
In interface builder, configure the tab bar settings, and the tabs, you may nest your UINavigationController in the tab bar.
bind the UITabBarController instance to the IBOutlet you created at step 1
change your applicationDidFinihedLaunching method to look like the following code.
(void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
}
Be sure to add the tabBar ABOVE your navigation bar in your view hierarchy. Depending on the structure of your user flow, you may end up with multiple navigationControllers.
tabBarController (w/ 3 sections, in this example)
|
------------------------------
| | |
navController1 navController2 navController3
Each navController maintains it's own stack.