我有一個自定義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