2013-03-10 58 views
1

我試圖做單組分選擇器例如開始的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 

請幫助提前 感謝:)

+0

是選擇器視圖的數據源,並在所有被稱爲委託方法正確地把它掛?確保你設置了選擇器視圖的'dataSource'和'delegate'屬性。 – rmaddy 2013-03-10 17:59:06

+0

嗨, 我設置了選擇器視圖的dataSource和委託屬性,但仍然不起作用 – Happy 2013-03-10 18:03:54

+0

正如我第一次問,數據源和委託方法是否被調用?在'numberOfComponentsInPickerView'中設置一個斷點或者添加一個'NSLog'。 – rmaddy 2013-03-10 18:05:08

回答

2

您需要的Alloc你NSArray的對象,如下所示:

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

編輯:

你還需要設置singlePicker.datasource =自我;和singlePicker.delegate = self;內的viewDidLoad或IB(包括委託和數據源)

-(void)viewDidLoad { 

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

仍然無法運行 – Happy 2013-03-10 18:27:08

+1

如果通過接口生成器進行連接,請確保連接數據源和委託。還要確保數據源和代理都在viewDidLoad中的.h文件 – BarryK88 2013-03-10 18:28:26

+1

中提及,您還需要設置singlePicker.datasource = self;和singlePicker.delegate = self; – BarryK88 2013-03-10 18:29:19