2013-06-02 33 views
1

我創建了一個類UIView的子類Word1View。在它中,我有一個UILabel testLabel,它是一個屬性,並且已經在.m文件中正確合成。 這裏是.h文件。爲什麼UILabel不會出現在子視圖中?

@interface Word1View : UIView 

@property (nonatomic, retain) UILabel *testLabel; 
@end 

類型Word1View的特性word1View在界面生成器創建並添加到我的故事板和已被添加作爲一個屬性到我的主視圖控制器:

@interface Board3ViewController : UIViewController 
@property (nonatomic, strong) IBOutlet Word1View *word1View; 

@end 

和已經正確地連接作爲IBOutlet中。

在我的視圖控制器的viewDidLoad方法是下面的代碼:

word1View.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 10)]; 
word1View.backgroundColor = [UIColor yellowColor]; 
word1View.testLabel.text = @"Working?"; 
[self.view addSubview:word1View]; 
NSLog(@"Label text is: %@", word1View.testLabel.text); 

現在我知道,這一切的代碼工作,因爲子視圖改變它的背景顏色和testLabel得到正確的文本,因爲它的印刷通過NSLog聲明。問題是UILabel testLabel在程序運行時不會出現?我試過setNeedsDisplay,並沒有幫助。 感謝任何幫助。謝謝

+0

你設置在主線程中的Text屬性? – nzs

+0

由NSLog語句正確寫入文本,以便正確設置 – Gil

+0

我看到'Word1View'的'UILabel'屬性,但是您將該標籤的實例設置爲'word1View'的子視圖? –

回答

1

根據我的評論,將標籤作爲子視圖添加到world1View對象。如果你想這在默認情況下出現,你可以在World1View初始化做到這一點:

-(id)init { 
    if ((self = [super init])) { 
     self.testLabel = [[UILabel alloc] 
      initWithFrame:CGRectMake(10, 10, 30, 10)]; 
     self.testLabel.text = @"Working?"; 
     [self addSubview: self.testLabel]; 
    } 
    return self; 
} 
+0

對於上述代碼中的任何錯誤抱歉 - 我的Objective-C語法仍然不是非常尖銳。雖然應該傳達信息! –

相關問題