无法在以编程方式创建的自定义视图

时间:2015-08-10 14:26:24

标签: ios objective-c uilabel custom-view

首先,我创建了一个自定义视图,其中包含UIImageViewUILabel

.h文件:

#import <UIKit/UIKit.h>

@interface ImageText : UIView
@property (strong, nonatomic) IBOutlet ImageText *view;
@property (weak, nonatomic) IBOutlet UILabel *money;
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end

这里是.m文件

    #import "ImageText.h"

    @implementation ImageText

    - (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
            self = [[[NSBundle mainBundle] loadNibNamed:@"ImageText" owner:self options:nil] objectAtIndex:0];
    }
    return self;
}

@end

那就是setLabelText方法。我在头文件中声明了它。我希望能够创建此自定义视图,然后更改UILabel的文字。

在我的ViewController .m我这样做:

ImageText *it = [[ImageText alloc] init];
[it.money setText:@"a"];


[_innercontent insertSubview: it belowSubview:_alishead];

[it setTranslatesAutoresizingMaskIntoConstraints:FALSE];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:it attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual toItem:nil
                                                      attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:59.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem: it attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual toItem:nil
                                                      attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:21.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_alishead attribute:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual toItem:it
                             attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_alishead attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual toItem:it
                                                      attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

它使用以下代码创建View并将其放在我想要的位置,但setLabelText方法什么都不做。我尝试过这样的事情:

it.money.text = @"a";

这似乎也不起作用。我该如何更改此文本?

我尝试删除了setter,尝试使它们强大而不是弱我试过[it.money setText:@&#34; a&#34;];但它都不起作用。

5 个答案:

答案 0 :(得分:0)

检查.h文件中的IBOutlet是否正确链接到xib文件中的UILabel

答案 1 :(得分:0)

我尝试做同样的事情,最后它起作用了。以下是我的代码

if (self) {
        self=[[[NSBundle mainBundle] loadNibNamed:@"YOUR_NIB_NAME" owner:self options:nil] objectAtIndex:0];
}
return self;

两者都可以使用 init或initwithframe

进行初始化

希望有所帮助

答案 2 :(得分:0)

你的代码片段很明显,你在方法initWithFrame中定义了nib加载:但是你用普通的alloc-init创建你的视图实例。您必须使用[[ImageText alloc] initWithFrame:#someFrameOrZeroFrame]创建它。否则你的笔尖将永远无法加载。

答案 3 :(得分:0)

将视图添加到头文件时,您应该选择视图而不是File's Owner选项。奇怪的是它如何说我应该在我看过的教程中选择文件的所有者。

答案 4 :(得分:0)

我认为这个问题可能是UILabel的错误参考,即Money。在标签上标注100,然后尝试通过viewwithtag将文本设置为标签。

UILabel * label =(UILabel *)[self.view viewWithTag:100]; label.text = @“你的文字”;

在您的情况下,self.view是ImageText的对象。

相关问题