2013-09-23 49 views
1

我有一個自定義UIView它是用xib構建的,其文件所有者名爲CustomModuleUIView,其中包含標籤和按鈕。我在Rootview控制器中調用了這個自定義視圖,並且我成功使用initWithCoder方法顯示它。問題是我無法從customMouleUIView或從根ViewController更改UILabel的默認文本。我找到了一個示例,告訴我在initWithCoder中進行自定義初始化,但它不適用於我,沒有任何更改,沒有任何錯誤,它顯示默認文本。更改自定義UIView的UILabel文本不起作用

這是我的自定義的UIView XIB enter image description here

這是我的根視圖控制器 enter image description here

這是我的代碼的.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"]; 
} 

即使在沒有負荷,我不能改變文本

+0

請發佈您的代碼。 –

+0

您是否已將xib中的標籤鏈接到視圖控制器中的相應IBOutlet? – joao

+0

我只能在8小時後才能添加評論,這是stackoverflow告訴我的。 – user2784013

回答

5

我發現我的錯誤,它是關於文件的所有者,我已經在檢查器中進行了更改,但通過選擇uiview而不是te文件的所有者,我將NSObject更改爲我的類名並重新連接標籤。

0

我猜文件的所有者屬性應該是你的「根視圖 - 控制」。

+0

我不能鏈接基於UIView與UIViewController的xib我認爲? – user2784013

+0

對不起誤讀......你有沒有嘗試過'[view.positionLabel setText:@「hello world」];'在你的設置方法中?或者'[_moduleCustomView.positionLabel setText:@「hello world」];'在viewDidLoad方法中? – matthias

+0

是的,這是我沒有做任何改變,昨天我嘗試awakeFromNib方法,我可以改變它,但我想動態地編輯它。 – user2784013