简体   繁体   English

更改自定义UIView的UILabel文本不起作用

[英]Change UILabel text of custom UIView doesn't work

I have a custom UIView which was build with xib that have a file owner called CustomModuleUIView which contains labels and buttons . 我有一个用xib构建的自定义UIView ,它有一个名为CustomModuleUIView的文件所有者,其中包含标签和按钮。 I have called this custom view in my Rootviewcontroller and I succeeded to display it using initWithCoder method. 我在Rootviewcontroller中调用了此自定义视图,并使用initWithCoder方法成功显示了该视图。 The problem is that I can't change the default text of UILabel neither from customMouleUIView nor from the root ViewController . 问题是我既不能从customMouleUIView也不能从根ViewController更改UILabel的默认文本。 I found example that tells me to do custom initialisation in in initWithCoder but it doesn't work for me and nothing changes and without any error it displays the default text. 我发现了一个示例,该示例告诉我在initWithCoder进行自定义初始化,但是它对我不起作用,并且没有任何更改,并且没有任何错误,它显示了默认文本。

This is my custom UIView xib 这是我自定义的UIView xib 在此处输入图片说明

This is my root view controller 这是我的根视图控制器 在此处输入图片说明

This is my code oh custom UIView .h 这是我的代码哦,自定义UIView .h

 #import <UIKit/UIKit.h>

@interface ModuleCustomUIView : UIView

-(void) setup;
@property (weak, nonatomic) IBOutlet UIImageView *_moduleIcon;
@property (retain, nonatomic) IBOutlet UILabel *_moduleName;
- (IBAction)openDemo:(id)sender;
- (IBAction)close:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *_moduleImage;
@property (strong, nonatomic) IBOutlet UILabel *_positionLabel;

@end

code of .m , i use setup method to init my UIView because I couldn't call .m的代码,我使用设置方法来初始化我的UIView,因为我无法调用

[[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil] ;

inside initWithCoder that causes infinite loop . 在initWithCoder内部,这会导致无限循环。

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
    }
    return self;
}

-(void) setup
{
    NSString *xib= @"CustomUIView";
    NSArray *array=[[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil] ;
    UIView *view =[array objectAtIndex:0];
    //code that doesn't work
    [_positionLabel setText:@"hello world"];
    //
    [self addSubview:view];
}

this is my root view controller .h 这是我的根视图控制器.h

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_moduleCustomView setup];
    [self.view addSubview:_moduleCustomView];
    //code doesn't work 
    [_moduleCustomView setText:@"hello world"];
}

even in the did load I can't change the text 即使在加载后,我也无法更改文本

我发现了我的错误,这是关于文件所有者的,我在检查器中更改了它,但是通过选择uiview而不是文件所有者,我将NSObject更改为我的类名并重新连接标签。

我猜Files Owner属性应该是您的“ root viewcontroller”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM