Adding/removing viewcontrollers view - iphone

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.

Related

Dismiss all UIViewController

I have app with 3 UIViewContoller
UIViewContollerA
UIViewContollerB
UIViewContollerC
UIViewContollerA open UIViewContollerB with presentModalViewController
UIViewContollerB open UIViewContollerC with presentModalViewController
And i want to have the possible that in one click on button in UIViewContollerC to dismiss all The UIViewController.
I try to call dismissModalViewControllerAnimated twice but it won't work, there is any other option to do it?
it's possible?
I would take a look at unwind segues. They allow you to unwind to any previous segue. The answers in this question are very helpful in describing how to use them: What are Unwind segues for and how do you use them?
Also as mentioned by Michael, modals weren't designed to be placed one on top of the other. I've found they work great for forms, but not much else. I would suggest that you use a UINavigation Controller as well. This will allow you to easily navigate back to the previous controller. Then you could also add a button for an unwind segue back to the initial view controller that you want.
You probably need some sort of chaining set of calls back up the modal stack to do this.
However, given you have a stack of view controllers, a UINavigationController might be a simpler solution to both presenting these view controllers in the first place and dismissing them down in one go.
Of course calling dismissModalViewControllerAnimated won't work, it's intended to be called on different view controllers. From the docs:
Dismisses the view controller that was presented by the receiver
So you'll need to set up delegation between the controllers in order to pass a request to dismiss the controller backwards to the initial controller.
Keep in mind that:
There's probably a better way of doing whatever it is you're trying to do. Modal controllers weren't intended to be presented on top of each other.
You're using deprecated methods. Use
presentViewController and dismissViewController instead
you would pass a reference of UIViewContollerB in UIViewContollerC, like this
#property(nonatomic, assign) UIViewContollerB *refB;
and call dismissModalViewControllerAnimated in UIViewContollerC to dismiss it, and fire dismiss action of UIViewContollerB in viewWillDisappear
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.refB dismissModalViewControllerAnimated:YES]
}
What you could try is to use dismissModalViewControllerAnimated with animated:NO. On the top most one and animated:YES on the controller below.
Or perhaps it workd, if you just call dismiss on the rootController?

How to switch between two view controllers

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?

Dismiss some views

First, sorry about my English, I know it's bad, but I'm trying to make it better...
Here is the issue: I'm making an app, with a view controller, with a lot of views... I want to make a button to go back to the main one. I think I have to dismiss the views I pushed. I dont know if that is necessary or if I can push it again directly. If it is, how can I do that? I've tried to put a dismiss method after the presentModalViewController one, but it didn't work.
Any help?
Thank you so much for your help ;)
It hard to tell from your question, but you may be confused by the navigation stack and the modal controllers.
A modal controller gets presented and dismissed using:
presentModalViewController:animated:
dismissModalViewControllerAnimated:
Navigation controllers are pushed and popped using:
pushViewController:animated:
popViewControllerAnimated:
So make sure you are using the correct method to pop or dismiss the view in question.
To present and dismiss a modal view you'll have to use delegation.
Here is an explanation for how to present and dismiss modal views.
Modal views are handled differently than views controlled by the UINavigationController or UITabBarController, which just push them onto a view stack or array of views.

viewWillAppear only being called once

Here's the scenario, switchViewController is the view added to the main window. So switchViewController is the main view, so if I want to go view B, I will addsubview of view B, there isn't a need to remove switchViewController's view right?
The issue is after I go back from view B to switchViewController's view, the method viewWillAppear is not being called anymore.
Why is it so?
viewWillAppear: is not called automatically when a view is removed from or added to the view hierarchy. It is the responsibility of the view controller to call it at the right time. The built-in view controller classes do this whenever you present or push a new view controller. Since you do not use this mechanism in your app, the method doesn't get called (unless you call it yourself).
That's because it never disappeared, you were just putting something else in front of it. If you want to navigate from one screen to another and back, they should be separate view controllers, and you should be using UINavigationController and its pushViewController:isAnimated: method.
It's not getting called beause your just modifying the first view, not navigating to a different one.
You might consider embedding your view in a Navigation controller, then calling your ViewB with
[navigationController pushViewController:viewB animated:YES];

iPhone SDK: How to display a view controller within another?

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.