2009-09-13 69 views
0

我在視圖上添加一個標籤 UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f,320.0f,300.0f,75.0f)]; [titleLabel setText:[BusinessLogic instance] .homeMessage];如何檢查控件是否已被添加到視圖?

然後我轉到另一個視圖,然後回來。這導致兩個標籤控件相互重疊。我想要做的是: 檢查是否已經添加了標籤控件。如果不添加並設置文本。如果是,請設置文本。

什麼是最好的辦法。我想學習正確的方法,因爲我已經有一些如何做到的令人厭惡的想法。

謝謝。 我

回答

3

您可以檢查上海華屬性:

if (titleLabel.superview == self) { 
} 

(假設「自我」是視圖要添加的標籤)

+0

我比如說,他應該首先將UILabel變量移到函數之外。然後檢查是否爲零。 – CiNN 2009-09-13 21:56:06

+0

我同意CiNN - 謝謝你們 – amok 2009-09-14 16:37:29

1
if (titleLabel.superview != someView) { 
    [someView addSubview:titleLabel]; 
} 
1

屬性到您的類加入存儲對您的標籤的引用。如果您不存儲任何地方,你不能輕易地在未來改變其文本:

// header 
@property (nonatomic, retain) IBOutlet UILabel *titleLabel; 
// implementation 
@synthesize titleLabel; 

創建UILabel例如,如果titleLabelnil,並分配給它:

if (self.titleLabel == nil) { 
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 320.0f, 300.0f, 75.0f)]; 
    self.titleLabel = titleLabel; 
    [titleLabel release]; 
    // add to the view here. 
} 
self.titleLabel.text = newText; 
+0

小錯字:@synthesis - > @synthesize – 2009-09-13 20:43:46

相關問題