我在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
答案 0 :(得分:2)
这里有三个可能的原因,都是相关的:
self.PickerView
是否实际指向某个对象总是一个好主意 - 如果它是nil
,你可能没有连接它,你的代码只会默默地丢弃这些消息你送到空的伊娃。另外,作为Objective-C样式的一小部分:按照惯例,变量,属性和ivars的名称以小写字母开头,而类和结构以大写字母开头。您不必遵循此约定,但它将使您的代码更容易为其他人阅读,并且它将帮助您反过来阅读其他人的代码。