1

我有一个UIView用 xib 构建的自定义,它有一个名为的文件所有者CustomModuleUIView,其中包含标签和按钮。我在我的 Rootviewcontroller 中调用了这个自定义视图,并成功地使用initWithCoder方法显示它。问题是我无法更改UILabelfromcustomMouleUIView或 from root的默认文本ViewController。我找到了一个例子,告诉我在 in 中进行自定义初始化,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 的代码,我使用 setup 方法来初始化我的 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"];
}

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

4

2 回答 2

5

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

于 2013-09-24T13:22:27.187 回答
0

我猜文件所有者属性应该是您的“根视图控制器”。

于 2013-09-23T09:43:14.010 回答