数组中的数据未显示在pickerView中

时间:2013-03-10 17:51:55

标签: iphone objective-c ios6 nsarray uipickerview

我正在尝试单组件选择器示例开始iOS 6开发手册,几乎每件事情都运行良好但数组中的数据没有显示在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

请帮助 在此先感谢:)

1 个答案:

答案 0 :(得分:2)

您需要按如下方式分配NSArray对象:

self.chaNames = [NSArray alloc] initWithObjects:@"Luke",@"Leia",@"Han",@"Chewbacca",@"Artoo", @"Threepio",@"lando"];

编辑:

你还需要设置singlePicker.datasource = self;和singlePicker.delegate = self;在viewDidLoad中或使用IB(委托和数据源)正确地连接它

-(void)viewDidLoad {

    self.chaNames = [NSArray alloc] initWithObjects:@"Luke",@"Leia",@"Han",@"Chewbacca",@"Artoo", @"Threepio",@"lando"];
}