用代码生成的uipickerview,数据显示不出来,datasource和delegate也设置了,还请各位帮忙看看 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; pickerView.delegate = self; pickerView.dataSource = self; pickerView.showsSelectionIndicator=YES; pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 50, 300, 400)]; NSArray *array = [NSArray arrayWithObjects:@"会撒娇看",@"时间",@"街道上空",@"都是", nil]; self.pickDate = array; [self.view addSubview:pickerView]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(50, 300, 30, 10)]; [button setTitle:@"按" forState:UIControlStateNormal]; [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [self.pickDate count]; } #pragma mark Picker Delegate Methods - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [self.pickDate objectAtIndex:row]; } - (void)show{ NSInteger row = [pickerView selectedRowInComponent:0]; NSString *selected = [pickDate objectAtIndex:row]; NSString *title = [[NSString alloc] initWithFormat:@"You selected %@!", selected]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"Thank you for choosing." delegate:nil cancelButtonTitle:@"You""re Welcome" otherButtonTitles:nil]; [alert show]; } |
|
40分 |
你看看你的6、7、8行代码。。。pickerView还是nil,你第9行才实例化的
|