If I wanted to perform some sort of action right after dismissing a modal view controller, where exactly would I put the code? I know it wouldn't be in viewDidLoad because the rootviewcontroller was already loaded into memory (because it was just temporarily hidden by the modal view) and I'm not sure about viewWillAppear or viewDidAppear because of course the view has already been loaded into memory.
ViewWillAppear will be called again in caller view controller when modal is dismissed, so with some logic can be a good place.
Otherwise you can think about implementing a delegate, or if synchronicity is not an option to be considered, you can send a custom NSNotification to be catched by the modal caller.
Related
I have a UIViewController sublcass (VC1) embedded in a UINavigationController. VC1 triggers a modal segue to another UIViewController subclass (VC2) which is embedded in its own, different UINavigationController. Inside of an action method triggered by a UIBarButtonItem in VC2's nav bar, I call
[self performSegueWithIdentifier:#"SomeString" sender:nil]
which corresponds to an unwind method inside VC1. For some reason, the transition does not occur.
It only became a problem after switching to XCode 6. It worked fine in XCode 5. Any ideas?
This issue has been doing the rounds and I have the exact same problem. Unfortunately there is no good solution to it yet, other than go back to the old delegate pattern.
If you subclass your parent view controller navigation controller and implement - (UIViewController*)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender you will see that the modal is actually looking for your unwindSegue method on the navigation controller instead of the view controller that presented the modal.
The problem gets even more amplified if you have a container view controller as the method above gets called all the way up the controller chain to the storyboard's initial view controller.
There's a potential workaround here Unwind Segue not working in iOS 8 but it has its downsides and side effects as well.
A normal answer would be, when the view will appear.
Say I have a UIViewController. Let's call that vc.
Say I want vc to control a view.
so I do vc.view = controlledView;
I expect everytime controlledView is about to appear then [vc viewWillAppear] will be called.
It doesn't.
What's wrong?
Also viewDidLoad is also not called even after I do
[vc view]
Technically, vc.view is already loaded
if you are calling viewWillAppear in any other file , with other View instance then it will never call.
viewWillAppear method calls every time when go to that view and if you leave it and then again come or come-back , it will call again.
please must use 'Super' keyword as.
[super viewWillAppear] in WillAppear method.
According to docs
This method is called before the receiver’s view is about to be added
to a view hierarchy and before any animations are configured for
showing the view. You can override this method to perform custom tasks
associated with displaying the view.
viewWillAppear is always called when your view is about to appear, as name itself suggests.
If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.
Life Cycle of view controller goes like this:
When a viewcontroller is allocated and loaded, loadView is called then viewDidLoad is called. You can see the entire flow as in image.
Refer to this image
NOTE: This image is taken from this answer
The right answer is the following.
Did you add the child viewController as the child of the parent child view controller.
viewWillAppear will only be called for the parent view controller (the top screen view controller) unless the other viewController is declared as the child.
Then the parentViewController will pass on viewWillAppear events.
How does iOs know?
Does each view has a pointer to it's controller?
What happened?
When we pop a viewController from navigation, does the navigationController arrange which view should be called?
For example:
If I added:
[[BNUtilitiesQuick window] addSubview:[BNUtilitiesQuick searchController].view];
viewWillAppear will be called.
ios does know which viewControler viewwillappear should be called even under cases in the question. There is no way I can think of how they know that without a pointer from view to viewcontroller.
However, window doesn't know the viewController. I am passing the view outlet of the controller not the controller. How can iOs 5 knows that it has to call [[BNUtilitiesQuick searchController] viewWillAppear:YES]
Navigation Controller maintains a stack of view controllers.
Once a view controller is popped, it is removed from the stack, and now the view controller exactly below this is the first view controller.This is how it works.
You can check documentation for more details - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html#//apple_ref/doc/uid/TP40011313-CH2
I'm not sure why this is happening, but in the App delegate, I'm setting a viewcontroller as the root and presenting a ModalViewController in front of it. The strange thing is that the viewcontroller behind the modalviewcontroller is still loading even though it does not appear. Is this normal? Is there a way to prevent the viewcontroller behind the modalviewcontroller to load?
Thank you
I am not sure exactly what you mean by "loading." As long as the view controller in the background is the parent view controller of the modal view controller, it is going to need be initialized before the modal view controller can be displayed at all. In that sense, it must be loaded.
If, on the other hand, you mean that the contents of the background view controller are visually displayed and then, afterwards, the model view controller appears on top of it—but you don't want the contents of the background controller to be seen until after the modal view controller is dismissed—then one possibility would be to set the hidden property to YES and unhide the contents when modal view controller is dismissed. To do that, I would make the background view controller a delegate of the modal view controller, so it receives a callback when the modal view controller is dismissed.
I have a viewController that presents a modalViewController when a UINavigationBarItem is touched. After the user is done with the modal view, the modalViewController is dismissed. At this point I want to change/remove the NavigationBarItem -- how would I accomplish this?
I was thinking of using viewWillAppear: but I only want the change to happen when the modalViewController is dismissed.
One way is to use NSNotificationCenter.
Before presenting the modal view, call addObserver to prepare to be notified.
At the place where the modal view is dismissed, post a notification using postNotificationName.
The notification will call a method you specify in addObserver.
In this method, put your "modal view dismissed" logic.