2017-06-20 25 views
1

我正在以編程方式創建標籤。不幸的是,我發現每次更新標籤時,我都會創建一個新實例。有沒有辦法在創建之前檢查UILabel是否已經存在?IOS/Objective-C:延遲加載UILabel

這是我當前的代碼來創建標籤:

UILabel *percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 280, 20)]; 

如果我調理它之後,我得到的錯誤,它並不能識別標籤:

for (id percentLabel in self.view.subviews) { 
     if (![percentLabel isKindOfClass:[UILabel class]]) { 
      UILabel *percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 280, 20)]; 
     } 
    percentLabel.textAlignment = NSTextAlignmentCenter; 

感謝您的任何建議。

+0

你把這段代碼放在哪裏? – AdamPro13

+0

現在它是在它自己的方法中,我做各種東西來計算百分比。當你改變會改變百分比的東西時,該方法被調用。 – user6631314

+0

當self.view沒有子視圖,它不會進入循環並且沒有創建UILabel,並且如果self.view有一些不是UILabels的子視圖,那麼將創建新的UILabel並且不會使用因爲當它存在if條件時,標籤實例被釋放,並且應用程序也會崩潰,因爲它試圖在沒有屬性textAlignment(即現在的代碼發生了什麼)的某種其他類型的視圖上查找textAlignment。如果self.view具有UILabel視圖,它將對齊文本。 – abintom

回答

1

有很多方法來檢查對象存在。在您的示例中,if語句將在for週期的每次迭代中創建一個標籤。該代碼的固定版本是這樣的:

UILabel *percentLabel = nil; 
for (id subview in self.view.subviews) { 
    if ([subview isKindOfClass:[UILabel class]]) { 
    percentLabel = (UILabel *)subview; 
    break; 
    } 
} 
if (!percentLabel) { 
    // Label creation code here 
} 

不過我建議你到那個標籤存儲在您的視圖控制器屬性和懶洋洋地初始化它就像下面這樣:

... 
@property (weak, nonatomic) UILabel *percentLabel; 
... 

- (UILabel *)percentLabel { 
    if (!_percentLabel) { 
    UILabel *percentLabel = /* initialization code here */; 
    // Add percent label as a subview to your view 
    _percentLabel = percentLabel; 
    } 

    return _percentLabel; 
} 

請注意,我使用弱參考來存儲屬性,因爲您的視圖實際上會擁有該標籤並通過它的subviews屬性保留強有力的參考。

+0

我得到它通過這種方法分配初始化標籤一旦在視圖中加載,然後將其存儲在屬性中,如下所示。然後我更新百分比label.text屬性作爲數據更改。 – user6631314

1

它在我看來像你循環你的子視圖(其中一些可能實際上不是標籤),你正在嘗試改變文本對齊方式。

有更好的方法來跟蹤標籤實例,然後在子視圖每一次循環,比如設置一個類屬性,如:

UILabel *labelToChange; 
在你的界面decleration

在.h文件中。

然後在你的實現只訪問使用

labelToChange.textAlignment = NSTextAlignmentCenter; 

只要確保標籤只[UILabel alloc] init一次初始化的UILabel

+0

現在我正在使用y原點(高度)的變量初始化標籤。也許我應該爲init的標籤賦值一個任意值,然後再改變它。但我希望有一種方法來延遲alloc初始化 – user6631314

+0

不幸的是,只是更改您在框架中使用的變量的值將不會更改標籤的框架。如果您想移動它,您必須明確地將標籤傳遞給一個全新的框架。 –

0

一個實例可以給percentLabel獨特的tag值。

那麼你可以使用viewWithTag得到percentLabel,像

UILabel *percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 280, 20)]; 
percentLabel.tag = 100; 
[self.view addSubview:percentLabel]; 

//In another method 
UILabel *percenLabel = [self.view viewWithTag:100]; 
if (percentLabel == nil) { 
    percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 280, 20)]; 
    percentLabel.tag = 100; 
    percentLabel.textAlignment = NSTextAlignmentCenter; 
    [self.view addSubview:percentLabel]; 
} 
else { 
    percentLabel.textAlignment = NSTextAlignmentCenter; 
} 
1

這將是更容易設置一個唯一的標籤,以您的UILabel這樣你就不需要檢查類。請記住,在UITableView,UINavigationBar等中甚至還有UILabel,而且你不想混淆這些。

因此,你應該做的是創建2個獨立的方法:一個創建標籤,並在viewDidLoad ONCE中調用,另一個是更新標籤。

創建方法:

-(void)createLabels { 
    UILabel *percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 280, 20)]; 
    percentLabel.tag = 1111; // a unique tag 
    [self.view addSubview:percentLabel]; 
} 

更新方法:

-(void)updateLabelWithText:(NSString*)newText { 
    UILabel *yourLabel = (UILabel*)[self.view viewWithTag:1111]; 
    yourLabel.text = newText; 
}