[英]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.