UIPopoverController displaying an UIPickerView

Discussion in 'iOS Programming' started by mycompuser, Apr 2, 2013.

  1. mycompuser macrumors member

    Joined:
    May 8, 2012
    #1
    Hello all,

    I am relatively new to iOS development.

    In my universal app, for iPad, I have an UIPopoverController over which I want to display an UIPickerView's object _pickerView.

    I create the UIPickerView object and set it's delegate and datasource as self and also implement the 3 datasource and 1 delegate method of UIPickerView.

    Now when I try to add the UIPickerView object as subview to to UIPopoverController object's view, I don't see the UIPickerView being displayed at all. I only see the blank popover control being shown.

    The code used to add the picker view to popoverController is provided below.

    Code:
    [self.popoverController.contentViewController.parentViewController.view addSubview:_pickerView];
    
    //note: popoverController is an object of UIPopoverController.
    
    What am I doing wrong for iPad?.


    I execute the below mentioned code for iPhone device which works fine.
    Code:
                [self.view.window addSubview:_pickerView];
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    That is not the usual way to set the content view of your popover. Where did you learn this?

    The usual complementary approach to a popover on the iPhone is to present the view modally.

    You obviously are favoring addSubview: as your solution to present new views. That is much less common nowadays. May I ask what resources you are using to learn iOS programming? (Please be as specific as possible).
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    In general, you should treat a view controller's views as private any time you have code that says

    Code:
    some_other_view_controller.some_subview
    It is a design error.

    You should create a custom subclass of UIViewController that has it's own XIB file (or scene in storyboard, if you're using storyboards) and manages a picker.

    Let's call the class MyPickerController.

    You'd then create an instance of MyPickerController and use it to initialize your popover. The code might look something like this: (adapted from one of our apps)

    Code:
      //Create the view controller you want to display.
      MyPickerController *thePickerController =[[PickSourcesViewController alloc] initWithNibName: nil bundle: nil]; 
      
      thePickerController.delegate = self; //Only needed if you need to manage the picker controller as it runs...
      
      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
      {
        //iPhone doesn't support popovers, so just display it as a modal.
        [theSourceVC presentViewController: thePickerController
                               animated: TRUE
                             completion: nil];
      }
      else
      {
        //create a popover controller to manage the popover, and give it our picker controller as a content VC
        currentPopover = [[UIPopoverController alloc] initWithContentViewController: thePickerController];
    
        thePickerController.thePopover = currentPopover; //only needed if the VC needs access to it's popover.
        [currentPopover setPopoverContentSize: thePickerController.view.bounds.size animated: NO];
    
        [self showPopover];
      }
    
     
  4. mycompuser thread starter macrumors member

    Joined:
    May 8, 2012
    #4
    Thanks for the reply.

    The above mentioned code snippet is an extract from the iPhone/iPad app which I am working on. There is an UI issue which I was trying to fix in the product.

    Did find few sample reference projects where they are using UIPopoverController and UIPickerView but separately.

    Could somebody share a link to some sample project demonstrating the required task which I can lookup as reference.

    Thanks & Regards.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    There may not be any examples out there. One of the skills of being a programmer is being able to combine separate-but-proven solutions into a single solution that solves your desired problem. So, you may have a solution for presenting a popover and you may have a solution for presenting a picker-view. You then need to apply your skills and talent to combine them.

    Again, I ask: what resources are you using to learn iOS programming?

    P.S. And did you read Duncan C's post?
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    See my code above. I posted code showing how to create a view controller and display it in a popover on iPad, and a modal on iPhone/iPod touch.
     

Share This Page