我在 Xcode 5 中为 iOS 7 实现了一个简单的标签和 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