1

我正在尝试做单组件选择器示例开始 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

请帮助提前谢谢:)

4

1 回答 1

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"];
}
于 2013-03-10T18:25:33.070 回答