[英]ios/xcode/objective-c: UIPickerView Not Displaying
我有一個UIPickerView,它顯示在情節提要中,但是如果我在編譯程序時更改背景顏色,則它是不可見的,或者最好是一個彩色塊。 我是否需要用數據填充它才能顯示,即編譯后它不再顯示庫比蒂諾,山景城等嗎?
順便說一句,我試圖用數據填充選擇器視圖,但仍然不可見:
code:
in .h file:
@interface pickerVC : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) IBOutlet UILabel *color;
@property (strong, nonatomic) NSArray *myArray;
in viewdidload;
UIPickerView *pickerView;
pickerView.delegate = self;
_myArray = [[NSArray alloc] initWithObjects:@"Blue",@"Green",@"Orange",@"Purple",@"Red",@"Yellow" , nil];
//PICKER METHODS
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// NSUInteger numRows = 3;
return _myArray.count;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = _myArray[row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
NSLog(@"Selected Row %d", row);
switch(row){
// Handle the selection
}
是的,您需要實現數據源和選擇器視圖委托。
是的,它沒有顯示。 您必須設置picker視圖的委托和數據源,並在controller.m文件中實現委托方法。 例如
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return @"ABC";
}
首先,在您的.h文件中添加此<UIPickerViewDelegate,UIPickerViewDataSource>
,之后再添加Delegate方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 5;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return @"xyz";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.