I am trying to switch between multiple different views using a UISegmentedControl. I found a really good example to do this in:
http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited
However, I need to use a UITabbarController in my app. So the navigation controller is not available for me in the app delegate. Is there any other way to switch views in a UITabbarController using UISegmentedControl?
First, consider whether you really want to do this. UITabBarController already gives you the ability to switch views, using the tab bar.
If you really want to do this for some reason, UITabBarController has properties viewControllers and selectedViewController that should allow you to do something like what is described in that article.
Related
I have an app planned out that needs to have a custom menu throughout the application. Its not a toolbar or anything like that so i don't think a regular UINavigationController or a UITabBarController will do the job.
What would be the best approach to creating this custom menu that appears in all views? I thought of just creating a view with the custom menu and alloc it for each view but it seems like a bit of an overkill. Extending UINavigationController might also be an option, but I'm not sure.
Would love to hear your opinions.
Thank you! :)
Shai.
The UINavigationController and the UITabBarController are pretty much always the best way to go because they have view and memory management built in. Here's what you can do:
Create a subclass of UITabBarController that hides the tab bar. See the last post on this page: http://www.iphonedevsdk.com/forum/iphone-sdk-development/4091-uitabbarcontroller-hidden-uitabbar.html Make this UITabBarController accessible on a singleton object.
Create a view for your menu and some IBActions corresponding to the menu buttons.
When a menu button is pressed, you can manually switch the tabs of the uitabbarcontroller as follows: tabBarController.selectedIndex = x;
I agree with ade. I think a popover controller added to a shared class would fit best to the iOS style (I'd put it in AppDelegate in order to have reference to it from anywhere and to avoid creating multiple instances and using only one which you will keep displaying / hiding whenever you wish to see the menu)
I can think of two options: 1. Subclass UINavigationController, hide the standard UINavigationBar's view and create your own view and put it on top of it (ugly and who knows what the results will end up like). 2. Add the menu as a subview of UIWindow so it stays on top of everything throughout the app.
I think the best way is to create a custom tool bar and use it across the app. Subclassing UINavBar is another option but not recommended by Apple so I would not go there.
I'd look into using a popover style menu such as WEPopover
I used the following tutorial Red Artisan to create a UISegmentedControl which I use to switch between two views: a simple one and a table view.
Everything is fine when I create a new standalone project, but what I really want is to incorporate this functionality in another project with a UITabBarController as the rootController (the segmented control with two views will be inside the second tab).
In the above tutorial the segmented control, navigation and segment controllers are instantiated and configured from within the application delegate.
Any idea how this could be done from a lower level?
Thank you in advance!
Basically, you need to create a UITTabBarController based application (There are LOTs of tutorials on how to do this), and then in the second UIViewController (or a custom subclass), you would create your UISegmentedControl to switch between two pages.
It sounds like you might also be using a UINavigationController as your base controller in the first application, if this is so..you could have a UITabBarController that has two Controllers in it (two tabs), and the second one will be a UINavigationController like how you had it setup in the app delegate.
Also, there are tutorials to do basically this exact thing all over the internet. Try searching for " UINavigationController inside UITabBarController " or similar. One other thing, there is an example of how the "layering" works in the Apple UI documentation for having a UINavigationController be one of the tabs of a UITabBarController (This is a quite common approach for iPhone apps)
Good luck!
In my app I would like to replace the TabBar with a ToolBar under certain conditions, similar to what happens in the Photos App when a user places it in selections mode (A toolbar with share copy, etc, buttons appear over the tab bar). How can I achieve this please?
This can be achieved by creating a new toolbar, assigning it an appropriate frame and adding it to self.tabBarController.view
I'm assuming your root view controller is a UITabBarController. Sometimes using the canned "Root" UIViewControllers is more of a hindrance than a help, especially if you want a highly custom look that does not fit into the paradigm of what the canned controllers offer. There's no reason you have to use them -- you could write your own, and do your own transition between your sub-UIViewController views onto the screen. You can use the UITabBar without the UITabBarController in your own custom UIViewController subclass, then you don't end up fighting the behavior of UITabBarController. Writing your own root ViewController can be very instructive as well -- you learn about all the things a root ViewController must do to manage the sub-ViewControllers.
I want to use TabBar and wnat to also have more than one views.
But then is it possible to have one navigationController for each views?
I want keep track of views in each of them, and ofcourse it should display different navigationBar and views when I tab a new Tabbar item.
Is that possible? I could give it a try but I don't want to waste time. So if it's possiblt, I'm going to start to work.
Thanks to any advice.
Short answer, yes, it's possible. :)
Longer answer: For each of the UIViewControllers, you would instantiate a UINavigationController and set the UIViewController as the root view using initWithRootViewController:. You would then pass these UINavigationControllers into the UITabBarController by setting the viewControllers property.
Situation:
I have an Xcode project based on the "Navigation-Based-Application" template. So that means I have a SINGLE UINavigationController that manages a UIViewController.
What I want To Do:
What I want to do is add one more UINavigationController to my project -- and be able to switch back and forth between them. (I want to do this to make space for some seperate unrelated content so that it does not have a back button pointing back to the root view controller.)
Question:
How do I add one more UINavigationController to my project and switch between the two UINavigationControllers?
The most common, and natural iPhone OS, way of doing this is to add a UITabBarController to your application. The Xcode template Tab Bar Application will guide you in the right direction on how to use it.
But...
If you don't like to have a Tab Bar in your application, and wish to switch between different UINavigationController instances (or any UIViewController for that matter), you can do something like this.
First you need to create your UINavigationController instances in a appropriate place (for example a new view controller, or in you Application Delegate, if you want to take the easy way out). You can then switch between controllers by just swapping which Navigation Controller's view that should be visible.
Example in the Application Delegate, "firstNavigationController" and "secondNavigationController" are UINavigationController instance variables:
- (void)showFirstNavigationController {
[secondNavigationController.view removeFromSuperview];
[self.window addSubview:firstNavigationController.view];
}
This will simply display the first instead of the second Navigation Controller. Note that this example is very simple. I didn't take into consideration that you should correctly handle the methods viewWillAppear:, viewDidAppear: and so on.