简体   繁体   English

为什么我得到一个黑框而不是选择器?

[英]Why am i getting a black frame and not the picker?

I have this very simple code or at least i think it's simple. 我有这个非常简单的代码,或者至少我认为它很简单。

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {  
CGRect rect = CGRectMake(0.0f, 20.0f, 320.0f, 216.0f); 

UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:rect]; 

self.view = myPickerView;
[myPickerView release];
}

I am running a general View Based template with XCode. 我正在使用XCode运行一般的基于视图的模板。 I can see the loadView getting called but i get an black frame instead of the UIPickerView. 我可以看到loadView被调用,但我得到一个黑框而不是UIPickerView。

What am i doing wrong ? 我究竟做错了什么 ?

/donnib / donnib

Have you implemented the picker's datasource methods? 您是否实现了选择器的数据源方法? You need it otherwise it won't show. 你需要它,否则它不会显示。

Link to Apple Documentation here: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Reference/UIPickerViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIPickerViewDataSource 链接到Apple文档: http//developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Reference/UIPickerViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIPickerViewDataSource

You've forgotten to set the UIPickerView's delegate to your current view. 您忘记将UIPickerView的委托设置为当前视图。 Add: 加:

myPickerView.delegate = self;

...following your initialization and before your view. ...在初始化之后和查看之前。 And of course, make sure you set up your header file as a UIPickerView delegate and implement the dataSource methods. 当然,请确保将头文件设置为UIPickerView委托并实现dataSource方法。

Try this: 尝试这个:

UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainscreen] bounds]];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame: CGRectZero];
CGSize pickerSize = [pickerView sizeThatFits: CGSizeZero];
pickerView.frame = [self pickerFrameWithSize: pickerSize];
[pickerView setShowsSelectionIndicator: YES];
[contentView addSubview: pickerView];
[pickerView release];
[self setView: contentView];
[contentView release];  

Instead of overriding -loadView , you should override -viewDidLoad . 相反,覆盖的-loadView ,你应该重写-viewDidLoad Once the view has loaded you'll create the picker and add it as a subview of the view owned by the view controller. 加载视图后,您将创建选择器并将其添加为视图控制器拥有的视图的子视图。

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect rect = CGRectMake(0.0f, 20.0f, 320.0f, 216.0f); 
    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:rect];
    [self.view addSubview:myPickerView];
    [myPickerView release];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM