繁体   English   中英

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

[英]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.

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