UILabebl 设置属性出错

我有一个View,它有一个UIImageView和UILabel

@interface NetStatusView()

@property (nonatomic,strong) UIImageView *statusImageView;

@property (nonatomic,strong) UILabel *statusLabel;

@end

然后我在设置属性的时候,报错了:

- (void)layoutSubviews{
    [super layoutSubviews];
  self.statusLabel.centerY = self.bounds.size.height * .5f;//这行报错了
  self.statusImageView.right = self.bounds.size.width;
  self.statusImageView.centerY = self.bounds.size.height * .5f;
}

报错是:

'NSInvalidArgumentException', reason: '-[UILabel setCenterY:]: unrecognized selector sent to instance 0x7feb3bd76bb0'

@不如笑归去 同学所说的没有初始化,应该是我没有贴代码,不好意思,贴上:

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)setup{
    self.backgroundColor = [UIColor clearColor];
    _statusImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _statusImageView.hidden = YES;
    _statusLabel     = [[UILabel alloc] initWithFrame:CGRectZero];
    _statusLabel.hidden = YES;
    _statusLabel.backgroundColor = [UIColor clearColor];
    _statusLabel.textColor = UIColorFromRGB(0xffffff);
    _statusLabel.font = [UIFont systemFontOfSize:16.f];
    [self addSubview:_statusImageView];
    [self addSubview:_statusLabel];
}

本人ios新手,这是对着某个教程写的,感觉没问题啊,请问这个问题有什么思路吗?(如果需要贴更多信息,请随便提)

阅读 3.6k
6 个回答

[UILabel setCenterY:] 说的是UIlabel里面没有这个Set方法, 但是有setCenter方法的, 你可以设置下这个Label的Center试试 CGpoint类型的 还有,我没看到你初始化的地方

给一个控件设置非只读属性值之前,一定要保证它已经被初始化。你上面没有初始化代码,问题就应该出现在这里。

self.statusLabel = [UILabel new];
//或者
self.statusLabel = [[UILabel alloc] init];
//再或者
self.statusLabel = [[UILabel alloc] initWithFrame:frame];

新手上路,请多包涵

unrecognized selector sent to instance 你以后会经常碰到这个的。。
这是运行时在方法列表找不到你调用的对应方法,经过3次转发就会报unrecognized selector sent to instance。
直接调center就行了。。人家的centerY是引入了自己的工具类,自定义的centerY。
希望对你有帮助。

新手上路,请多包涵

unrecognized selector sent to instance 表示你没有实现这个方法,
你是不是在分类.h里声明setCenterY方法,并没有在.m里实现这个方法

self.statusLabel.centerY,UILabel原生没有-setCentY:方法,一般都是因为UIView增加了frame相关的category

新手上路,请多包涵

不能直接设置center的Y,需要将center的Y包装在CGPoint中通过label.center来设置。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题