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 に答える