我有一个自定义UIView
它是用xib构建的,其文件所有者名为CustomModuleUIView
,其中包含标签和按钮。我在Rootview控制器中调用了这个自定义视图,并且我成功使用initWithCoder
方法显示它。问题是我无法从customMouleUIView
或从根ViewController
更改UILabel
的默认文本。我找到了一个示例,告诉我在initWithCoder
中进行自定义初始化,但它不适用于我,没有任何更改,没有任何错误,它显示默认文本。更改自定义UIView的UILabel文本不起作用
这是我的自定义的UIView XIB
这是我的根视图控制器
这是我的代码的.m的哦自定义的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
代码,我使用设置方法来初始化我的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"];
}
即使在没有负荷,我不能改变文本
请发布您的代码。 –
您是否已将xib中的标签链接到视图控制器中的相应IBOutlet? – joao
我只能在8小时后才能添加评论,这是stackoverflow告诉我的。 – user2784013