ios - 纯代码创建的UIPickerView,数据源和代理方法为何不被调用?
阿神
阿神 2017-04-18 09:05:37
[iOS讨论组]

学习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、删除底部工具条里其他的控件,失败。

阿神
阿神

闭关修行中......

全部回复(1)
PHP中文网
    UIPickerView *unitPicker = [[UIPickerView alloc] init];
    self.unitPicker.dataSource = self;
    self.unitPicker.delegate   = self;

self.unitPicker还是nil呢…… 你丢了一句self.unitPicker = unitPicker;……

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号