I have a navigationController, I push a new viewController onto it's stack, I have been trying to figure out a way to switch this ViewController with another viewController from within the first ViewController.
I have 3 almost identical views, depending on the user interaction I would like to be able to switch between these views within the same stack index i.e. without pushing or popping views on the navigationController.
So if view C has index 3 on the navigationController stack and the user taps a certain button in view C the C1 view replaces C on the stack and gets index 3.
I have been through the UINavigation Class and can't find a way around this.
If I popViewController from within the current viewController and the tries to pushViewController… well that can't be done as the viewController gets released when popping it. If I instead try to do pushViewController, well then I gets added on top of my current viewController.
I hope it makes sense and that someone can help me out:)
Thanks
You can assign an NSArray of controllers to the viewControllers property to alter the entire stack instantaneously.
Related
I'm writing an app with two or three main features. So I use Tabbarcontroller to switch between viewcontrollers (VC). The problem is, if I switch from VC1 to VC2, and then switch back to VC1, content of VC1 has changed when it moves from background to the foreground.
Does it mean that this is a newly instantiated VC, different from the VC1?
If so, how can I keep VC1 and push it to the front without creating a new VC?
I'm trying to search for the solutions but I don't know which keyword to begin with.
Please help me. Thanks.
The default behavior of UITabBarController is to hold references to the tabs (the ViewControllers) in it´s viewControllers property. If you switch back and forth the viewDidAppear() method is called on the ViewController for the selected tab.
The controllers managed by a UITabBarController will be allocated while the TabController is allocated. After that it will use the same instances of the controllers.
You may update some UI in viewDidAppear?
For reference: UITabBarController
I have two UIViewControllers, vc1 and vc2.
I want to switch between them. But before loading the view of the new view controller, I want to destroy/release/remove (I'm not sure abt the correct word to use here) the previous viewcontroller.
For example, when I am switching to vc2 from vc1 ,I want to destroy vc1 completely, so that when I return to vc1 from vc2, vc1 will be loaded from scratch (i.e. viewDidLoad will be executed).
Which type of view switching should I opt for?
presentModal...
addSubview.
I am not using a navigation controller.
Currently I am using the presentModal... method, but when I use dismissModalViewcontroller on the newly presented view controller, it doesn't show up a new instance of the previous view controller. Instead, it shows the already running instance of it.
I want the viewDidLoad method of the previous view controller to run when I dismiss the newly presented view controller.
What exactly needs to happen in viewDidLoad?
You also have viewWillAppear available to you, so it could be that you could move the required functionality there and still use the modal presentation.
See this answer. You can do this with or without animation.
Animate change of view controllers without using navigation controller stack, subviews or modal controllers?
If I instantiate a lets say, UISegmentedControl. And link each button to add a viewcontrollers view to the viewhierachy, would it be enough to just add that view to the stack or must I call other methods aswell. And what about removing views? Would it be sufficient to just say
I do NOT have the correct syntax in front of me right now so bear with me.
[self removeSuperview];
Is it necassary to call methods on the viewcontroller itself?
Use a NavigationController and either push or present Modally the views. The you can just pop or dismiss them.
I have 10 views with UINavigationController in hierarchy. Now when I want to go back from 10 to 9, and so on with back button, which method should I use?
popviewController
popToViewController
I have set a fixed value for all views. At application launch, I get that value. For that value I initialize rootviewController with use of UINavigationController. And my rootviewController is nothing but the first view and after that on next line, I'm pushing a particular view.
If you're pushing the views onto a UINavigationController, you don't need to explicitly set any method for the back button that the UINavigationController object manages, since it will pop the correct view when tapped. It's all automatic.
Fundamentally, what I want to do is within, for example, ViewControllerA display ViewControllerB and ViewControllerC. How would I do that?
Thanks in advance.
You don't display view controllers, you display views. Having said that, you can do something like this:
UIViewController *a = ...;
UIViewController *b = ...;
[a.view addSubview:b.view];
Now, having said that, you shouldn't do it. Tons of stuff does not behave properly, because there are tons of undocumented interactions between UIView, UIWindow, and UIViewController. There is nothing in the documentation that says it won't work, but random things stop behaving properly (viewWillAppear: on the interior view's VC doesn't get called, etc).
If you need this functionality, you should file a bug with Apple.
The default template for a navigation view controller should do what you want assuming you want two different screens (not two different sections on the same screen). Whenever you want to change the view from the current one to another, just tell the navigation controller to push it on the stack:
[self.navigationController pushViewController:viewBoards animated:YES];
The default navigation view controller gives you a root view controller with a navigation view controller in it. It also gives you one view controller called MainWindow. Just add as many copies of MainWindow as you need to get your functionality.