2014-01-14 34 views
0

我已經創建了多個UILabel,每個都有自己的.tag值。然後,我嘗試使用另一個類的NSString填充UILabel,該類調用的方法來自使用.tag設置UILabels的方法。但是,當我嘗試訪問我的調試部分中的標籤時顯示標籤爲空,文本從未設置。使用標記符號訪問UILabel

這就是我的方法看起來像被稱爲從另一個calss。

- (void) SymbolButtonPressed:(NSString *)selectedString { 

    UILabel *label = (UILabel *)[cutField viewWithTag:currentlySelectedTag]; 
    [label setText:selectedString]; 

} 

標籤時我調試的代碼= NULL ...我不知道爲什麼

的要求,這是我怎麼加標籤

axesView = [[UIView alloc] init]; 
axesView.frame = CGRectMake(0.0, 0.0, scrollWidth+10, 77.0); 


cutField = [[UILabel alloc] initWithFrame:CGRectMake((i*40)+2, 35, 40, 40)]; 
cutField.textColor = [UIColor blackColor]; 
[cutField setTextAlignment:NSTextAlignmentCenter]; 
cutField.font = [UIFont fontWithName:@"Helvetica" size:20]; 
cutField.backgroundColor=[UIColor whiteColor]; 
cutField.layer.borderColor = [[UIColor colorWithRed:colorController.grRed/255.0 green:colorController.grGreen/255.0 blue:colorController.grBlue/255.0 alpha:1.0] CGColor]; 
cutField.layer.borderWidth = 0.5f; 
[axView addSubview:cutField]; 
+1

是標籤,你正在尋找,cutField的子視圖? – thbonk

+0

你試過了嗎(UILabel *)[self.view viewWithTag:currentlySelectedTag];?也許你正在向self.view添加提到的UILabel。你可以在你添加標籤的地方發佈代碼嗎? – vburojevic

+0

什麼是cutField?如果您試圖通過標籤訪問的視圖與具有SymbolButtonPressed:方法的視圖不在同一個控制器中,那麼您需要先獲取對其他控制器的引用,然後才能訪問其任何視圖。 – rdelmar

回答

1

當其他人在暗示,您幾乎總是希望在調用viewWithTag時使用視圖控制器的內容視圖。該方法將遍歷所有子視圖,子視圖和子子視圖,直到找到您的標記。通過使用self.view,無論標記視圖位於視圖層次結構中哪個位置,它都能正常工作,即使您稍後將視圖移動到不同的子視圖中(這比您想象的更頻繁)

+0

好吧我現在試試self.view。 – HurkNburkS

+0

好吧,這工作完美!感謝那! – HurkNburkS