iPad and UIPickerView (or UIDatePickerView)
Has anyone had any luck using a UIPicker in the 3.2 SDK? I'm in the middle of porting an iPhone application over to an iPad and that's the one thing I can't seem to get to work. I've tried...
-Creating an action sheet, add the picker as a subview and displaying it.
-Creating that above action sheet, making it the view of a generic ViewController, adding that VC to a UIPopover
-Making just the picker the view of a generic V开发者_开发百科iewController, adding that VC to a UIPopover
With the action sheet it doesn't even attempt to draw it. In the popover view it attempts to draw but doesn't get rendered correctly.
Just wanted to check to see if anyone has accomplished this and if so how.
Thanks everyone!
I just struggled with this for about 24 hours and got it to work by following this tutorial and wherever he mentions his UITableView, just think about your UIPickerView and replace the appropriate info.
Matthew Casey - Tutorial: Introduction to Pop Over Control on iPad
In short, on the iPad, the ONLY way you can show a picker is inside a popover control. Sharing data between the popover's content view controller (the picker view's view controller) and the main app's view controller is the bit I couldn't figure out. (i.e. getting the picker's "didSelectRow" method to affect an object in your main view controller, say a UITextField or UIButton.) This is handled by creating a custom protocol to forward the "didSelectRow" method to the main view controller.
I'm by no means an expert at this stuff, so I went step by step with that tutorial and it DID work in the end. Before I found that tutorial I was lost. (I am also porting over an iPhone app to iPad.)
I am the author of the tutorial mentioned above. For a picker to work in a popover and add the selected value to the textbox in the main view you need to use protocols. I have created a sample project to show this. I will be creating a tutorial on this soon.
Sample Project
When you want to share data between view controllers ... the solution is the NSNotificationCenter.
I know this doesn't answer the question in whole, but if I want to share data between view controllers: In the sending view controller
NSMutableDictionary *toshare = [[NSMutabledictionary alloc] init];
[toshare setValue:valueToShare forKey:@"shared"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"imSharing" withObject:nil userInfo:(NSDictionary *)toshare];
In the receiving view controller: Place in the viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imSharing:) name:@"imSharing" object:nil];
then add this function:
-(void)imSharing:(NSNotification *)notification {
NSDictionary *dictionary = [notification userInfo];
//do something with [dictionary objectForKey:@"shared"];
dictionary = nil;
}
This works for sharing data between 2 view controllers... well, small amounts of data anyways.
Otherwise look here for more about sharing data between view controllers
Are you using Interface Builder? It's a lot easier to create a new UIViewController subclass in Xcode with the nib file, and then drag in the UIPicker. Then you need to present the view controller in a popover. Action sheets are not designed to have subviews, so don't do that.
I have done it: see the third screenshot in C64 Paint.
Thanks for the responses everyone. So while I can't say what the initial problem was, I started the problem from scratch and got it to work. I made completely new View Controllers and displayed them in a Popover and it worked well. Seems to be one of the tougher points of making a universal app.
The UIPickerView will work if you create it on the iPad if you create it like so:
UIPickerView* pickerView = [[UIPickerView alloc]initWithFrame: CGRectMake (someX, someY, 320, 216)];
Some other sizes do appear to work. But CGRectZero does not work on the iPad -- even though it does work in the iPhone.
精彩评论