【发布时间】:2013-09-23 09:18:20
【问题描述】:
我有一个自定义的UIView,它是用 xib 构建的,它有一个名为 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 的代码,我使用 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"];
}
即使在加载时我也无法更改文本
【问题讨论】:
-
请发布您的代码。
-
您是否将 xib 中的标签链接到视图控制器中的相应 IBOutlet?
-
我现在只能在 8 小时后添加评论,这是 stackoverflow 告诉我的。
-
我已将 xib 和标签与文件所有者中的 IB 链接起来
标签: ios objective-c ipad uiview xib