ios / xcode / objective-c:UIPickerView不显示

时间:2015-09-15 11:42:50

标签: ios storyboard uipickerview

我有一个UIPickerView显示在故事板中,但如果我在编译程序时更改背景颜色,则它是不可见的或最多是彩色块。我是否需要填充数据才能显示,即编译后是否会再显示Cupertino,Mountain View等?

顺便说一句,我试图用数据填充选择器视图但仍然不可见:

    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
    }

3 个答案:

答案 0 :(得分:2)

是的,您需要实现数据源和选择器视图委托。

答案 1 :(得分:1)

是的,它没有显示。您必须设置选择器视图的委托和数据源,并在controller.m文件中实现委托方法。 e.g。

- (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";
}

答案 2 :(得分:0)

首先添加此内容  您的.h文件中的<UIPickerViewDelegate,UIPickerViewDataSource> 之后添加委托方法

- (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";
}