2011-03-21 75 views
1

我有一個UILabel,它不會在第一次加載時更新,但會在每次其他後續加載時正確加載。iPhone UILabel在第一次加載時不會更新

以下是代碼片段。同樣,正如我所說,在第一次加載時,它只會顯示xib中標籤中指定的內容,即label,並且第二次加載字符串label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";

-(IBAction)showDetails:(id)sender{ 
    NSLog(@"Annotation Click"); 
    self.userProfileVC.title=((UIButton*)sender).currentTitle; 
    if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"]) 
     self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books"; 

    [UIView beginAnimations:@"Curl Page Down" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
         forView:self.view cache:YES];  

    //Adjust the Subview by butting up to status bar 
    self.userProfileVC.view.frame = CGRectMake(0, 0, 320, 480); 
    //Add userProfileVC as a subview 
    [self.view addSubview:userProfileVC.view]; 
    [UIView commitAnimations]; 
} 

回答

0

我相信那時的你在第一時間訪問self.userProfileVC.label,該label仍然nil因爲視圖本身尚未加載。快速解決方法是,您必須首先在showDetails方法的開頭處添加self.userProfileVC.view;語句來加載視圖。

+0

謝謝你的伴侶,你給我的線索我只是移動視圖加載第一! – 2011-03-21 16:42:00

0

推測當你點擊你的UIButton時會調用這段代碼。所以在第一次UI加載時,我不會期望你的方法被調用(因爲沒有按鈕被按下)。你爲什麼不嘗試設置viewDidLoad或類似的默認文本?

例如沿着線的東西:

- (void)viewDidLoad { 
    self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books"; 
} 
+0

已加載什麼上面到標籤,但現在的信息是所有註釋一樣大?我有52個如果陳述有不同的信息我將如何去解決這個問題:-) – 2011-03-21 15:36:57

+0

「所有的註釋」 - 不知道你的意思,因爲我不明白你在做什麼,你從來沒有提到過之前的註釋。如果你的意思是你有很多'if isEqualToString:@「Asda」'類型的東西,考慮將標籤 - >註解映射存儲在一個NSMutableDictionary(或普通的NSDictionary)中。然後,您可以通過標籤文本查找註釋,而不需要大量的「if」。 – occulus 2011-03-21 17:35:18

0

我是這樣一個白癡我改變了我的代碼的順序....在標籤之前加載視圖。 DOH!

繼承人的片段:

- (IBAction爲)showDetails:(ID)發送{

//Must load the view before the annotations update the label with the correct label text! 
[UIView beginAnimations:@"Curl Page Down" context:nil]; 
[UIView setAnimationDuration:1]; 

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
         forView:self.view cache:YES];  

//Adjust the Subview by butting up to status bar 
self.userProfileVC.view.frame = CGRectMake(0, 0, 320, 480); 
//Add userProfileVC as a subview 
[self.view addSubview:userProfileVC.view]; 
[UIView commitAnimations]; 

NSLog(@"Annotation Click"); 
self.userProfileVC.title=((UIButton*)sender).currentTitle; 
if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"]) 
    self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books"; 
+0

你可能打算接受提亞的答案,而不是發佈自己的答案。如果您想澄清如何最終解決問題,可以編輯您的問題,並在最後加入「解決方案部分」。 – Kalle 2011-03-21 17:39:33

相關問題