学习UIPickerView后,想尝试用纯代码方式创建一个UIPickerView,关键语句如下:
#import "Masonry.h"
#import "ViewController.h"
@interface ViewController () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,weak) UIView *bottomView;
@property (nonatomic,weak) UIPickerView *unitPicker;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//创建一个底部工具条
UIView *bottomView = [[UIView alloc] init];
[bottomView setBackgroundColor:[UIColor colorWithRed:0.133 green:0.959 blue:0.959 alpha:0.600]];
[self.view addSubview:bottomView];
//创建UIPickerView
UIPickerView *unitPicker = [[UIPickerView alloc] init];
self.unitPicker.dataSource = self;
self.unitPicker.delegate = self;
[bottomView addSubview:unitPicker];
//用Masonry自动布局:
[unitPicker mas_makeConstraints:^(MASConstraintMaker *make){
make.left.equalTo(unitLabel.mas_right).offset(3);
make.height.equalTo(@80);
make.width.equalTo(@80);
make.centerY.equalTo(bottomView);
}];
}
//UIPickerView列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
NSLog(@"%s",__func__);
return 1;
}
//UIPickerView行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSLog(@"%s",__func__);
return 2;
}
//UIPickerView标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"%s",__func__);
return @"a";
}
实际运行的时候没有警告,但是Output窗口一片空白,所有的__func__都没有被调用。UIPickerView布局成功,但除了有三条分割线外全部都是空白,求教是哪里出了问题?
个人已经尝试过:
1、不用Masonry自动布局,改用initWithFrame方法创建UIPickerView,失败;
2、直接在self.view里添加UIPickerView,失败;
3、删除底部工具条里其他的控件,失败。
self.unitPicker
还是nil
呢…… 你丢了一句self.unitPicker = unitPicker;
……