我试图做单组分选择器例如开始的iOS 6发展的图书,几乎每一件事工作正常,但数据从数组没有出现在pickerView 请帮助数据没有显示在pickerView
了这里是我的.h文件
<UIPickerViewDataSource , UIPickerViewDelegate>
{
IBOutlet UIPickerView *singlePicker;
NSArray *chaNames;
}
@property (strong, nonatomic) IBOutlet UIPickerView *singlePicker;
@property (strong, nonatomic) NSArray *chaNames;
- (IBAction)buttonPressed:(id)sender;
@end
这里是.m文件
#import "SSPSingleComponentViewController.h"
@interface SSPSingleComponentViewController()
@end
@implementation SSPSingleComponentViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.chaNames = @[@"Luke",@"Leia",@"Han",@"Chewbacca",@"Artoo", @"Threepio",@"lando"];
}
- (IBAction)buttonPressed:(id)sender {
NSInteger row = [self.singlePicker selectedRowInComponent:0];
NSString *selected = self.chaNames[row];
NSString *title = [[NSString alloc]initWithFormat:@"You slected %@", selected];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"OutPut is : " message:title delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [self.chaNames count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return[self.chaNames objectAtIndex:row];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
请帮助提前 感谢:)
是选择器视图的数据源,并在所有被称为委托方法正确地把它挂?确保你设置了选择器视图的'dataSource'和'delegate'属性。 – rmaddy 2013-03-10 17:59:06
嗨, 我设置了选择器视图的dataSource和委托属性,但仍然不起作用 – Happy 2013-03-10 18:03:54
正如我第一次问,数据源和委托方法是否被调用?在'numberOfComponentsInPickerView'中设置一个断点或者添加一个'NSLog'。 – rmaddy 2013-03-10 18:05:08