UIPickerView不能显示数据

iOS 码拜 7年前 (2015-05-01) 944次浏览 0个评论

用代码生成的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];
}

UIPickerView不能显示数据

UIPickerView不能显示数据
40分
你看看你的6、7、8行代码。。。pickerView还是nil,你第9行才实例化的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明UIPickerView不能显示数据
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!