Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mycompuser

macrumors member
Original poster
May 8, 2012
37
0
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];
 
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?.

That is not the usual way to set the content view of your popover. Where did you learn this?

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

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).
 
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];

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];
  }
 
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).

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.
 
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.

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?
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.