简体   繁体   中英

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 . I have called this custom view in my Rootviewcontroller and I succeeded to display it using initWithCoder method. The problem is that I can't change the default text of UILabel neither from customMouleUIView nor from the root ViewController . 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.

This is my custom UIView xib 在此处输入图片说明

This is my root view controller 在此处输入图片说明

This is my code oh custom 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

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

inside initWithCoder that causes infinite loop .

-(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

- (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”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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