0

please help to understand the difficult situation.

I realized the Pop Up Window, the way has been described here

Then, in this window, I decided to implement a sample photo from the library, but when you open a new controller may receive the following warning:

Presenting view controllers on detached view controllers is discouraged <PopUpViewController: 0x7fbb405f4e90>.

The sample photo is implemented as follows:

picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = NO;
        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:picker animated:YES completion:nil];

3 Answers3

0

try this:

[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
Masterfego
  • 2,648
  • 1
  • 26
  • 32
0

Perhaps you need to add your popup window as a child controller using addChildController to the controller you show it from so that it is not detached from a view hierarchy?

Also you could try: Another option:

[self.parentViewController presentViewController:picker animated:YES completion:nil];

I noticed this on another thread as a solution. Warning :-Presenting view controllers on detached view controllers is discouraged

In your case, parentViewController may well be nil.

Edit After Seeing Code:

After looking at the code, the problem seems to be as follows:

1) The popup mechanism is using an instance of a UIViewController to create a layout, but you are not actually using the view controller at all. The popup mechanism (showInView:withImage:withMessage:animated:) is extracting the view from the controller and adding it into the view hierarchy of the controller asking for the popup. So using code from this popup controller instance which calls [self presentViewController...] is presenting a modal controller from a controller which is not actually being used. This is where the error is coming from I believe. You need to do all alerts and modal presentations from the original controller OR pass in the controller to use.

2) The original article mentions that because only the container view of the controller is used, you need to keep a strong reference to it when you create it. So you should have:

self.popViewController = [[PopUpViewController alloc] initWithNibName:@"PopUpViewController" bundle:nil];

Solution:

I would extend the showInView... method to pass in the controller which owns the view the popup is being spliced into. Then use this controller to present any modal controllers. So change:

- (void)showInView:(UIView *)aView withImage:(UIImage *)image withMessage:(NSString *)message animated:(BOOL)animated

to

- (void)showInController:(UIViewController *)aController inView:(UIView *)aView withImage:(UIImage *)image withMessage:(NSString *)message animated:(BOOL)animated

Store the controller reference passed in a property in your PopUpViewController class then use this property to call presentViewController...

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • I tried all the solutions that are described there - did not help – Alexey Slivkin Apr 08 '15 at 11:54
  • I notice in your code you posted, that you do not maintain a strong reference for the popup controller. I think you want to create a strong property for the popup and have `self.popViewController = [[PopUpViewController alloc] initWithNibName:@"PopUpViewController" bundle:nil];` This is mentioned in a few blog comments on the code you used. – Rory McKinnel Apr 08 '15 at 13:46
  • Updated after looking at your code you kindly posted. – Rory McKinnel Apr 08 '15 at 14:07
0

I think you are presenting your picker in your viewDidAppear method.

If this is so, then that is the reason for the warning...

Please write another method that will present our picker and then perform it with delay.

[self performSelector:@selector(yourMethod)
           withObject:nil afterDelay:0.0];

This will let your presentingController load completely before it presents another one.

I hope this helps..

EDIT

The problem is, you are presenting the picker, much before your action sheet is dismissed

EDIT 2

Also instead of actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex, use this method for getting clicked Index -

- actionSheet:didDismissWithButtonIndex:
Mayur Deshmukh
  • 1,169
  • 10
  • 25