Remove viewcontroller & move to next view controller - ios

Hi i have been working on an iOS app.What i am doing is navigating among diffrent view controllers. But the problem is i want finish the current view controller from emoery and then move to the next view controller.
I am using `[self.view removeFromSuperview]; for finishing the cureent view & using
self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:self.loginView animated:NO completion:nil];
for moving to next view controller but the thing is i am not able to remove it from memory.
Please tell me how can i do it?
Thanks in advance.
`

It's better to create a container view controller which manages your view controllers. For example, in viewDidLoad: of container controller you add current controller:
[self addChildViewController:self.currentViewController];
[self.currentViewController didMoveToParentViewController:self];
[self.view addSubView:self.currentViewController.view];
//here set up currentViewController view's frame or constraints if needed
When you need to open login controller, do this:
[self addChildViewController:loginViewController];
[self.loginViewController didMoveToParentViewController:self];
[self.view addSubView:loginViewController.view];
//here set up loginViewController view's frame or constraints if needed
//then remove current view controller
[self.currentViewController willMoveToParentViewController:nil];
[self.currentViewController removeFromParentViewController];
[self.currentViewController.view removeFromSuperview];

Remove from superview will remove it from the current view, but OS won't remove it until he needs to (this is topic for more explanation, let's say it won't remove it asap).
If you want something deleted just call it nil:
self.view = nil;
This will make the pointer to nil, so view won't be there any more. (the view really will be somewhere but you won't have access to it)

I am revising your code
self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:self.loginView animated:NO completion:nil];
What you are doing here is presenting your login viewcontroller.
self: This is the instance of the viewcontroller you are currently working on. So how could you remove self from memory. (Not Possible)
You can approach alternate ways.
For example: 1. Changing root view controller
Pop to root view controller and then Push Login View controller.

If you try to remove not only UIView but the whole UIViewController from a navigation controller stack use this snippet
NSMutableArray *stack = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
[stack removeObject:yourController];
self.navigationController.viewControllers = stack;
Be aware of using this only when you've already pass to the next controller view.
UPD:
Oh, now I see what you are trying to do. And I can't figure out, why you're trying to step your controllers this way (modally). I think you should use UINavigationController with navigation segues defined directly from your storyboard. Look at this article where apple purely explains what navigation is. That article is about modal views

Related

viewDidAppear and viewWillAppear methods are not called

I have a programmatically created UIViewController named as "VC" and on top of that I need to load my existing UIViewController.
I used below code to do that, and it's working fine.
I can see my existing UIViewController on "VC" but not detecting any of viewDidAppear or viewWillAppear in existing view controller.
I am getting data from viewDidAppear and viewWillAppear so all the time my existing view controller collection view is empty.
ExistingViewController* presObj= [self.storyboard instantiateViewControllerWithIdentifier:#"oad"];
[vc.view addSubview:presObj.view];
[self addChildViewController:presObj];
[presObj didMoveToParentViewController:self];
Have you initialize the storyboard class?
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
so all the time my existing ViewController collection view is empty --> are you using collection view in your code?
First you need to add the addChildViewController. Then add that childviewcontroller view into your main view.
Just add the [self addChildViewController:presObj]; before adding to the view subview.
ExistingViewController* presObj= [self.storyboard instantiateViewControllerWithIdentifier:#"oad"];
[self addChildViewController:presObj];
[vc.view addSubview:presObj.view];
[presObj didMoveToParentViewController:self];
Ok, If you are loading like this then your ViewDidLoad () of existing view controller will load, and you can put a condition (if needed) then you can call ViewDidAppear () ViewWillAppear() like below
existingviewcontroller *obj=[[existingviewcontroller alloc]init];
[obj viewWillAppear:YES];
[obj viewDidAppear:YES];
This will work

Dismiss and Present same view controller

i have a view controller and i need to dismiss it and present it back in same time.
i had tried dismiss it and call back the view controller but not working.
[self dismissViewControllerAnimated:YES completion:nil];
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
ExpandViewController *expandView=
[storyboard instantiateViewControllerWithIdentifier:#"ExpandViewController"];
expandView.delegate=self;
[expandView setEventDict:dict];
[self presentViewController:expandView animated:YES completion:NULL];
I am not exactly sure what outcome/functionality you are looking for in your question, but #matt is correct. However, you may be looking to have this happen seamlessly. Therefore you could use child view controllers instead of presenting the view controller using the [self presentViewController:VC animated:animate completion:nil] method.
Adding child vc:
[self addChildViewController:myVC];
[self.view addSubview:myVC.view];
[myVC didMoveToParentViewController:self];
Removing child vc:
[myVC willMoveToParentViewController:nil];
... remove subview.
You can set up a delegate between the two controllers to tell the parent when to dismiss the view to make things easy. You can also add the subviews at different indexes using [self.view insertSubview:myVC atIndex:index] or the other possible functions such as the insert above subview etc, to have one subview be added before dismissing the other to give a more seamless transition.
Hope this helps!
You can't present a view controller until the currently presented view controller has finished being dismissed. You won't know this has happened until the completion handler from your dismissal is called. Your mistake is that the completion handler is nil. Instead, provide a completion handler (in your first line), consisting of the remaining lines of your code. Thus, they will execute after the dismissal finishes.
[self dismissViewControllerAnimated:YES completion:^{
// ... the rest of your code goes in here ...
}];

How to switch between view controllers and get rid of the previous one

In android, switching between activities, is fairly straightforward
you call
Intent intent = new Intent(this,NextActivity.class); <- define the next activity
startActivity(intent); <- start the next activity
finish(); < -get rid of the current activity
now in iOS i know how to do this:
UIViewController *nextviewcontroller = [[UIViewController alloc]initWithNibName:#"nextvc" bundle:nil];
[self presentViewcontroller:nextviewcontroller animated:YES completion:nil];
How do I get rid of the current view controller? so that currentviewcontroller dies after presenting nextviewcontroller ?
[self dismissViewController:YES]; doesnt seem to do the trick
the lifecycle methods viewWillDisappear and viewDidDisappear are called even if I don't call [self dismissViewController:YES];
i want "currentviewcontroller" to be removed from the memory, and from the viewcontroller stack, so that clicking "back" in "nextviewcontroller" will go to some thirdviewcontroller that was before currentviewcontroller
In iOS is different, since there's no concept of Activity and everything is more focused on the app itself (in Android you can mix activities from different apps). Therefore, there's no concept of "view controller stack".
The most similar concept is the "navigation stack" of navigation controllers, where you actually push and pop new view controller into some kind of linear navigation. A navigation bar is automatically created and populated with back buttons.
presentViewController will show your view controller modally upon the current one, but you can't thrash the presenting one since it's holding and containing ("defining context") the new one.
If you use a navigation controller for your navigation hierarchy (I don't know if you can), you can override the back button and use something like
UIViewController * prev = self.navigationController.viewControllers[self.navigationController.viewControllers.count -2 ]
[self.navigationController popToViewController:prev animated:YES]
With a modal view controller, you may try something like (I haven't tried but it may work)
[self.presentingViewController.navigationController popViewControllerAnimated:YES]
You should write one of these code into the target action of your close button.
iOS doesn't maintain a global stack of controllers in the way that Android does. Each app shows a controller at its root, and that one is responsible for showing the other controllers in the app. Controllers can display other controllers modally using presentViewcontroller:animated:completion: but the presenting controller remains underneath the presented one.
If your current controller is the root controller, then instead of using presentViewcontroller:animated:completion: you'd just do this:
self.view.window.rootViewController = nextViewController;
It's very common for the root controller to be a UINavigationController, which does manage a stack of controllers. If that is the case, and if your current controller is at the top of the stack, you'd do this:
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:nextViewController animated:YES];
If your setup is different, you'd do something different; it's hard to say what without knowing more. But it's most likely that you'd be in the UINavigationController case.
In the viewDidAppear of your nextviewcontroller you could add :
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSArray *controllers = self.navigationController.viewControllers;
NSMutableArray *newViewControllers = [NSMutableArray arrayWithArray:controllers];
[newViewControllers removeObjectAtIndex:[controllers count]-2];
self.navigationController.viewControllers = newViewControllers;
}
There is nothing available like this in iOS but you can achieve it doing something like below
NSArray *viewControllers=[self.navigationController viewControllers];
NSMutableArray *newControllers=[[NSMutableArray alloc] init];
for(int i=[viewControllers indexOfObject:self];i<viewControllers.count;i++){
[newControllers addObject:[viewControllers objectAtIndex:i]];
}
[self.navigationController setViewControllers:[[NSArray alloc] initWithArray:newControllers]];
I have tried the method of storing all the view controllers in an array but it didn't work for me . When you try popViewController it will move to the View Controller which is last in the stack.
You can make 2 navigation controllers and switch between them and also switch between the view controllers of a particular Navigation Controller.
For eg.
You can switch between 2 Navigation Controller using the following code:
FirstNavController *fisrtView=[storyboard instantiateViewControllerWithIdentifier:#"firstnavcontroller"];
self.window.rootViewController = firstView;
}else{
SecondNavController *secondView=[storyboard instantiateViewControllerWithIdentifier:#"loginnavcontroller"];
self.window.rootViewController = secondView;
}
If your FirstNavController has 2 ViewControllers then you can switch between them using pushViewController
SecondViewController *sc = [self.storyboard instantiateViewControllerWithIdentifier:#"secondviewcontroller"];
[[self navigationController] pushViewController:sc animated:YES];
and popViewController
[self.navigationController popViewControllerAnimated:YES];

How can I interact with a View I just showed using presentViewController?

I've got a view that I use for adding data into a table. I show this view using presentViewController:
AddViewController *avc = [self.storyboard instantiateViewControllerWithIdentifier:#"addview"];
[self presentViewController:avc animated:YES completion:nil];
This UINavigationController is the root view controller for a UIViewController that displays the add controls.
I trigger this UINavigationController from a couple of different places in my project and I'd like to have slightly different text for each. How can I interact with the UIViewController from where I call presentViewController, or otherwise change UIViewController depending on where it was invoked?
Ok, you can access the rootView controller of your navigation controller like this:
UINavigationController *yourNavigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerID"];
YourRootController *yourRootController = (YourRootController*)[yourNavigationController topViewController];
yourRootController.someproperty = someValue
Than, just change some desired property in the view controller that you just get.
Please, tell me if it was something like this that you were looking for.
UIViewController *controller= (UIViewController)[self.navigationController subviews] count-2 ];
controller is the second-last view controller on the stack. which is the one that called add.
depending upon the value for [controler class] , you can set the text.

I am really confused regarding few things in UIViewController

I am really confused regarding few things in UIViewController, I have already read the View Controller Programming Guide and searched lot on the Internet but still confused.
When I want to jump or switch from firstVC to secondVC how many types of methods are available? I am listing which I know:
UINavigationController
UITabBarController
presentModalViewController:
Add secondVC to root view
If secondVC is added to root view then how firstVC object will be released?
Is it a good practice to add every view I want to jump/switch to root view?
transitionFromView:
I dont understand this portion from Apple doc:
This method modifies the views in their view hierarchy only. It does
not modify your application’s view controllers in any way. For
example, if you use this method to change the root view displayed by a
view controller, it is your responsibility to update the view
controller appropriately to handle the change.
If I do this:
secondViewController *sVc = [[secondViewController alloc]init];
[transitionFromView:self.view toView:sVc.view...
Still viewDidLoad:, viewWillAppear:, viewDidAppear: are working fine: I don't need to call them. So why did Apple say this:
it is your responsibility to update the view controller appropriately to handle the change.
Are there any other methods available?
Actually the standard methods used are :
1) Using NavigationController
//push the another VC to the stack
[self.navigationController pushViewController:anotherVC animated:YES];
//remove it from the stack
[self.navigationController popViewControllerAnimated:NO];
//or presenting another VC from current navigationController
[self.navigationController presentViewController:anotherVC animated:YES completion:nil];
//dismiss it
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
2) Presenting the VC
//presenting another VC from current VC
[self presentViewController:anotherVC animated:YES completion:nil
//dismiss it
[self dismissViewControllerAnimated:YES completion:nil];
Never use the method you described in points 4. It's not a good practice to change root view controller's dynamically. window's root VC is usually defined on applicationdidfinishlaunchingwithoptions after that it shouldn't be changed , if you are to follow apple standards.
Example for transitionFromView:toView
-(IBAction) anAction:(id) sender {
// assume view1 and view2 are some subviews of self.view
// view1 will be replaced with view2 in the view hierarchy
[UIView transitionFromView:view1
toView:view2
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished){
/* do something on animation completion */
}];
}
}

Resources