UIPopoverController delegate not getting called on dismiss - ios

I assign popover object o it's contentViewController and I put dismiss code in a button which resides in the content view controller.
When the button is pressed:
[self.popover dismissPopoverAnimated:YES];
is called and popover is dismissed.
However, delegate's method is not called automatically. I thought that I was not setting it's delegate, but it is there. If I add the following line after dismissPopoverAnimated line, delegate is called correctly...
[self.popover.delegate popoverControllerDidDismissPopover:self.popover];
I don't understand why it doesn't "automatically" call the delegate's method.
How can this happen?

The popoverControllerDidDismissPopover: in the delegate is not called when 'dismissPopoverAnimated:' is used.
From the Apple Documentation for popoverControllerDidDismissPopover: in UIPopoverControllerDelegate:
The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated: method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated: method.

There are two ways to dismiss a popover. (a) tapping outside the popover; and (b) doing it programmatically with
[self.popover dismissPopoverAnimated:YES];
If you do it programmatically, then the docs (https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIPopoverControllerDelegate/popoverControllerDidDismissPopover:) say:
The popover controller does not call this method in response to
programmatic calls to the dismissPopoverAnimated: method. If you
dismiss the popover programmatically, you should perform any cleanup
actions immediately after calling the dismissPopoverAnimated: method.
Thus, not calling the delegate automatically is the normal behavior, and what you're doing (calling it yourself) is fine.

popoverControllerDidDismissPopover is not called on Dismiss, but its called when you click outside the popoverController contentview.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html#jumpTo_4

Related

Call delegate when user tap "Back" button

I need a way to change text depending of user input, when user tap back button. I followed that solution: Find out if user pressed the back button in uinavigationcontroller?
and did add following code in viewDidLoad:
if ([self isMovingFromParentViewController]) {
NSLog(#"isMoved");
[self.delegate stringChangedTo:self.myTextField.text atIndex:self.indexToPass];
}
However, nothing changed. More to say, method is not called (NSLog dont output a string).
How could i find a way to call delegate when user tap back button?
That code needs to be in viewWillDisappear: or viewDidDisappear:. not viewDidLoad.
viewDidLoad is called when the view controller's view is loaded. You want to call the delegate when the view controller is being dismissed.
There is also a UINavigationControllerDelegate protocol. You can get notified when a given view controller is shown by implementing either of these:
-navigationController:willShowViewController:animated:
-navigationController:didShowViewController:animated:
ADDENDUM:
In my opinion, using the delegate is a cleaner design, because you get notified precisely when a navigation event occurs. View controller life cycle methods such as -viewDidDisappear:, etc. can get called when you present/dismiss a modal view controller, and require that you add logic to discern those.

viewWillDisappear is not called

I'm using a navigationController in my app. One UI i have tried to click one button (Not in Navigation bar) viewWillDisappear is not called. But i'm not using the navigationController it's calling the viewWillDisappear. Can you please help me how to call the viewWillDisappear with a navigationController?
So, first of all - (void)viewWillDisappear:(BOOL)animated is a method specific to UIViewController class.
Second, this method shouldn't be called directly from the code, this method is called automatically when a UIViewController is removed from a view hierarchy. (check UIViewController specs)
So in order to handle this method call, you have to implement it in your custom UIViewController class (do not forget to call [super viewWillDisappear:animated]). Whenever your custom UIViewController view will disappear from the screen (is popped from the stack or other UIViewController is added to the stack) this method will be called.

Dismiss a popover from itself programatically

I'm making a UIPopoverController and displaying it. All is well, content is loading... except I need to dismiss the popover when a button is pressed. The UIPopoverController's delegate is set to self but I don't know how to call a method in the parent view controller.
UIPopoverController has method
- (void)dismissPopoverAnimated:(BOOL)animated
so you can dismiss it directly from itself.
Unless you meant that you want to dismiss popover from its contentViewController, then there is at least dozen of answers regarding that here - for instance similar to this one

how to detect a popover dismiss

When a popover is open it closes if you click of it. Is is possible to stop this? Basically i have the following problem.
When a popover closes i need a function to be processed. If i set a button with a dismiss function from the popover then i can put the function in the dismiss method however this isn't detected if the user clicks of the screen.
So is is possible to stop a popover closing if you click off it.
or
Is it possible to detect this allowing the function i require to be called.
Thanks
James
Can use these two methods, but the second one suits your scenario:
/* Called on the delegate when the popover controller will dismiss the popover.
Return NO to prevent the dismissal of the view.
*/
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController;
/* Called on the delegate when the user has taken action to dismiss the popover.
This is not called when -dismissPopoverAnimated: is called directly.
*/
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController;
Remember to implement the UIPopoverdelegate and set the Delegate to self.
Assuming you are talking about the default behavior of popovers being dismissed when you tap outside of the popover, the solution to prevent this is to make the popover modal.
To do this, set the modalInPopover property to YES on the popover's content view controller.
BTW - if you want to detect when a popover is dismissed by the user by tapping outside of it, you need to implement the UIPopoverControllerDelegate method popoverControllerDidDismissPopover:.

Why popping to root view controller results in calling viewDidLoad sometimes?

I have a navigation based application and in the child view I have a button, tapping on which results in calling the popToRootViewController method.
-(IBAction)popToRootViewController
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
This should result in calling the viewWillAppear method of the rootViewController and it is happening in most of the cases. However, occasionally viewDidLoad of rootViewController is called. I am not able to find the reason behind it. Does any one has any idea why viewDidLoad is called sometimes?
On iOS 5 and Earlier, the System May Unload Views When Memory Is Low:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
viewDidLoad is called once when view controller's view is loaded first time.
viewWillAppear will be called after viewDidLoad method when view controller's view is loaded first time.
Now when ever u push or pop controller in navigationController, the visible controller's viewWillApper method will be called surely.
viewDidLoad, as the name implies, is called just after a view controller has loaded its view. If a view controller is no longer the frontmost controller, it may release its view to save memory (and it used to call viewWillUnload and viewDidUnload which are now deprecated in iOS 6). If this happens, when it comes to front again (or whenever something calls thecontroller.view), it will recreate the view (if is not Nib-based, it will call loadView), and then call viewDidLoad.

Resources