7

I wanted to reuse the popover for iPhone described in this video which is exactly what I need.

The problem is that I couldn't bind a UIViewController property to the popover's UIViewController like in the video.

One difference with the video is that it has been made using XCode 4.2 and I'm using XCode 5.

So the question is: How to make a popover for iPhone like in the video on XCode 5?

Here is the XCode 5 project I am struggling with.

Bug
  • 2,576
  • 2
  • 21
  • 36
Sèb
  • 471
  • 2
  • 7
  • 17
  • I tested this popover feature on iPhone 4s iOs8.1 simulator and I got a black screen blocking the app :( apparently the code below is not working anymore from iOs8 ... – Sèb Jan 11 '15 at 14:20

1 Answers1

14

I figured out a way to get popover to work on iPhone and iPad programmatically !

  • Create a category to make popover available on iPhone (more details here)

    //UIPopover+Iphone.h
    @interface UIPopoverController (overrides)
    + (BOOL)_popoversDisabled;
    @end
    
    //UIPopover+Iphone.m
    @implementation UIPopoverController (overrides)
    + (BOOL)_popoversDisabled { return NO;
    }
    @end
    
  • Create the button which will show the popover and implement the method it calls

ExampleUIViewController.h

@interface ExampleViewController : UIViewController <UIPopoverControllerDelegate>
    @property (strong, nonatomic) UIButton *detailButton;
    @property (nonatomic, retain) IBOutlet UIPopoverController *poc;

UIPopoverController poc has to be held in an instance variable, more details here.

ExampleUIViewController.m

- (void)viewDidLoad {
    _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_detailButton addTarget:self
                action:@selector(showPop:)
      forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_detailButton];
}

-(void)showPop:(UIButton *)button {
   UIViewController *detailsViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
   self.poc = [[UIPopoverController alloc] initWithContentViewController:detailsViewController];
   [self.poc setDelegate:self];
   [self.poc presentPopoverFromRect:_detailButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
  • Create the UIViewController that will contain what's displayed inside the popover (called DetailsViewController in the example)

Simply create it in your project by a right click -> New File -> Objective c class -> UIViewController and tick the box "With XIB".

Then a popover will appear right next to the button when tapped.

Tested OK on iOs5 and above.

Community
  • 1
  • 1
Sèb
  • 471
  • 2
  • 7
  • 17
  • how to add any object to this pop over view – chandru Dec 18 '13 at 07:35
  • The content of the popover, in my example, is DetailsViewController. So to add anything into the pop over view, add it to your DetailsViewController. – Sèb Dec 19 '13 at 05:54
  • Ya, I got, I used this, [myViewController.view addSubviews:objects]; – chandru Dec 19 '13 at 06:01
  • In http://stackoverflow.com/questions/18205903/popover-controller-for-iphone it has been accepted by Apple. As for myself I didn’t submit my app to the App Store yet. – Sèb Apr 06 '14 at 21:06
  • 1
    I tested this popover feature on iPhone 4s iOs8.1 simulator and I got a black screen blocking the app :( apparently this code is not working on iOs8 and probably all versions above... – Sèb Nov 11 '14 at 11:52
  • Hey Seb, I am also seeing black screen on iOS 8.1 simulator, did you resolve the issue? – Vinayaka Karjigi Mar 24 '15 at 08:10
  • I ended up using an other solution : I made the view appear above the others and set some animations (fading and translation) so it looks nice when the view comes to the screen. – Sèb Mar 25 '15 at 14:46