uiview to Uiview switch - iphone

I have a uiview class and i want to switch to other uiview class but without using addsubview
is there any other way to do that except the one (addSubView)

If you have UIViewControllers for the corresponding UIViews, and have a UINavigationController to handle your view flow, you can use pushViewController message at the mentioned navigation controller.

Since you have a navigation controller you can use pushViewController:animated to add anotherViewController to the navigation stack. You can also use presentModalViewController:animated. The section on Navigation Controllers in View Controller Programming Guide explains this very well.

Related

UITabBarContoller --> UISplitViewContoller

I want to show full screen view controller from UISplitViewController detail view controller. But I want UITabBar still to be visible. When I presentViewController it will hide my UITabBar. Can anyone suggest me the flow?
You don't have to nest your UIViewController in a UINavigationController.
Then instead of
[someViewController presentViewController: anotherViewController];
you have to call
[someNavigationController presentViewController:anotherViewController];
this will do the trick.
Note that it will add the anotherViewController at the top of the stack of viewControllers.
to remove it just call dismissViewController on the navigation controller!

UIView embedded only in UINavigationController and UITabBarController, why?

In iOS, why can a UIView (and others) be only embedded in a UINavigationController or UITabBarController? What's special about these two classes?
Edit: oops, wanted to ask 'UIViewController' instead of UIView.
No, its not true. UIView is embedded with UIViewController. This class provides life cycle for UIView. It takes responsibility from initializing the view to deallocating the view.
UINavigationController and UITabBarController are just derived from UIViewController. They provide extra functionality for building hierarchy and switching between hierarchy respectively.
UIView can be embedded in any ViewController or its subclass. UINavigationController and UITabbarController are nothing but subclasses of UIViewController.
I think the embedding you're referring to is the embed in menu item which only allows for UITabBarController and UINavigationController. This means that XCode will take your UIViewController subclass and embed it in one of these two controllers. They are special because they are controllers of other controllers (collections of UIViewControllers). Xcode is simply taking some of the pain out of building a view controller and then adding it to a navigation controller or a tab bar. You can easily embed it in one of these with one click and no code. Much easier than in past versions of XCode.
If you're talking about "embed" as in Interface Builder, yes, as of iOS 5, Interface Builder only gracefully designs user interfaces for three view controller containers, UINavigationController, UITabbarController, and UISplitViewController. These are the three container controllers that come out of the box. You can, though, do your own view controller containment. See Session 102 in WWDC 2011 for information on view controller containment. Also refer to the section on view controller containment in the UIViewController Class Reference.

access UINavigationController from subclass of UINavigationBar

I have a UINavigationBar subclass, is it possible someway from that UINavigationBar to get my UINavigationController?
This is not a good design pattern. You should put the navigation logic into your view controllers. If you have controls (such as buttons) in your custom navigation bar, you should also handle these events in your view controllers.
Note that the UINavigationBar does not deal with the controllers (it should be the other way round), rather it deals with the UINavigationItems, which will look different according to if they are on top of the navigation stack or not.
There is a trick to make your custom navigation bar the navigationBar property of your view controller's navigation controller. Normally this is a read-only property. But you can put it into your navigation controller in Interface Builder in Xcode, and then change the class name in the inspector to your custom class. You will then be able to refer to your bar with the usual self.navigationController.navigationBar from within your view controller.

Calling pushViewController from child controller function

I have a root view controller that subclasses UINavigationController. It loads in a child view with a UIButton. When that button is pressed I want to make a call from the child view's corresponding view controller (lets say ChildViewController) to the UINavigationController's pushViewController: method in the parent controller.
How is this possible without directly referencing the parent view controller? Is it achievable using a standard protocol method or do I have to create my own?
Every UIViewController has a property called navigationController. If a UIViewController is a part of a UINavigationController's stack, you can use the navigationController property in the following manner:
[self.navigationController pushViewController:yourNextViewController animated:YES];
There's no need to access the rootViewController only for pushing a new ViewController on the stack. This could get really awful if you had big navigation stacks.
By the way - Apple states that UINavigationController is not intended for subclassing. Usually, it is a good idea to listen to their warnings and directions, so you may want to revisit the subclassing approach again.
using a subclassed UIViewController which is loaded to the UINavigationController's stack may prove a better approach.
Hope this helps.
The child UIViewController that contains your child UIView and UIButton should have the parentViewController property. You can use that to get a weak reference to your UINavigationController where you can message pushViewController:animated:

What is the best way to change my UIViewController to a UINavigationController

I just realised that my 'root' viewController should have been a UINavigationController. Now I want to change this to be a UINavigationController instead and just curious what my best option would be. I built this view and all other views using IB if that makes a different.
I'm mostly worried that I would have to do a lot of copy/pasting and recoding to get everything right or will be it be as easy as manually editing my controller and change the extension to UINavigationController.
Thanks
You do not convert an existing view controller into a navigation controller.
Even though UINavigationController is a subclass of UIViewController, it's task is the management of other view controllers, not the management of views themselves. You don't swap them out one for the other. Instead, you set UIViewControllers to be controlled by the nav.
To add a nav to a project in IB, open the xib and drag over a UINavigationController. Then set the navigation controller's rootControlller property to the existing UIViewController.
And you're done.
You can also "Embed" the Navigation Controller by selecting the View Controller in IB then select Editor->Embed In->Navigation Controller
You can instantiate a trivial not-subclassed UINavigationController and give it your original UIViewController as its root controller, like:
YourRootViewController *rootViewController = [[YourRootViewController] initHoweverYouInitIt];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
and maybe an [rootViewController release];, depending on how you are going to manage memory.
UINavigationController has an 'is-a' relationship with UIViewController, so you should be able to change its class type in Interface Builder with no additional changes.