UIPickerView没有显示任何内容

时间:2013-08-13 16:44:06

标签: ios objective-c uipickerview

我在iOS 7的Xcode 5中实现了一个简单的标签和UIPickerView。我使用了以下网站的代码。一切似乎都是正确的。不知道为什么它不起作用。

http://www.youtube.com/watch?v=CIhqiuG8p1k

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UILabel *label;
    IBOutlet UIPickerView *Picker;
    NSArray *PickerData;
}

@property (retain, nonatomic) IBOutlet UIPickerView *Picker;
@property (retain, nonatomic) NSArray *PickerData;
@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize Picker,PickerData;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four", nil];
    self.PickerData = array;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component {
    return [PickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component {
    return [self.PickerData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component {
    int select = row;
    if (select == 0) {
        label.text = @"One Is Selected";
    } else if (select == 1) {
        label.text = @"Two Is Selected";
    } else if (select == 2) {
        label.text = @"Three Is Selected";
    } else if (select == 3) {
        label.text = @"Four Is Selected";
    }
}

@end

1 个答案:

答案 0 :(得分:2)

这里有三个可能的原因,都是相关的:

  1. 选择器视图是否确实存在?您是否已将其添加到定义此类接口的xib文件中?
  2. 你的xib中的选择器视图是否连接到选择器视图的插座ivar?在调试这样的问题时,检查self.PickerView是否实际指向某个对象总是一个好主意 - 如果它是nil,你可能没有连接它,你的代码只会默默地丢弃这些消息你送到空的伊娃。
  3. 选择器视图的数据源和委托属性是否连接到视图控制器?这就是选择器视图如何知道要求其内容的人。
  4. 另外,作为Objective-C样式的一小部分:按照惯例,变量,属性和ivars的名称以小写字母开头,而类和结构以大写字母开头。您不必遵循此约定,但它将使您的代码更容易为其他人阅读,并且它将帮助您反过来阅读其他人的代码。