繁体   English   中英

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

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

我有一个用xib构建的自定义UIView ,它有一个名为CustomModuleUIView的文件所有者,其中包含标签和按钮。 我在Rootviewcontroller中调用了此自定义视图,并使用initWithCoder方法成功显示了该视图。 问题是我既不能从customMouleUIView也不能从根ViewController更改UILabel的默认文本。 我发现了一个示例,该示例告诉我在initWithCoder进行自定义初始化,但是它对我不起作用,并且没有任何更改,并且没有任何错误,它显示了默认文本。

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

这是我的根视图控制器 在此处输入图片说明

这是我的代码哦,自定义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

.m的代码,我使用设置方法来初始化我的UIView,因为我无法调用

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

在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];
}

这是我的根视图控制器.h

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

即使在加载后,我也无法更改文本

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

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

暂无
暂无

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

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