我創建了一個類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,並沒有幫助。 感謝任何幫助。謝謝
你設置在主線程中的Text屬性? – nzs
由NSLog語句正確寫入文本,以便正確設置 – Gil
我看到'Word1View'的'UILabel'屬性,但是您將該標籤的實例設置爲'word1View'的子視圖? –