2012-03-09 87 views
1

工作在ViewController我有以下幾點:的UILabel的setText不viewWillAppear中

- (void)viewWillAppear:(BOOL)animated 
{ 
    DataObject *theDataObject = [self theAppDataObject]; 
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [formatter setDateFormat:@"MMM dd, yyyy HH:mm"]; 
    NSString *dateStr = [formatter stringFromDate:theDataObject.deadline]; 
    NSLog(@"Logged dateStr: %@", dateStr); 
    [dateTimeLabel setText:dateStr]; 
    [super viewWillAppear:animated]; 
} 

澄清:dateTimeLabelISxib文件接線。該viewWillAppear方法明確地從另一個ViewController叫,併發射,像這樣:

- (IBAction)setDateTimeButtonClicked:(id)sender 
{ 
    DataObject *theDataObject = [self theAppDataObject]; 

    theDataObject.deadline = [datePicker date]; 

    FirstMobileViewController *mobileVC = [[FirstMobileViewController alloc] init]; 
    [mobileVC viewWillAppear:YES]; 
    [mobileVC release]; 

    [UIView transitionWithView:self.view.superview 
         duration:0.5 
         options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionAllowAnimatedContent 
        animations:^{[self.view removeFromSuperview];} 
        completion:NULL]; 
} 

viewWillAppear方法燒製 - 在dateStrNSLog適當地記錄當上海華再次顯示。但dateTimeLabel從未更新。顯然,評論NSLog系列沒有什麼區別。

狂攬的事情是,即使NSLog日誌dateStr就好了,如果我改變dateStr,比方說,@"Yo!"甚至到本地初始化字符串,然後dateTimeLabel將更新,沒有問題。

缺少什麼我在這裏?

+0

你確定'[super viewWillAppear:animated];'需要在最後被調用,而不是在實現的開始?嘗試將調用移動到實現的第一行,看看它是否有所作爲。 – dasblinkenlight 2012-03-09 04:22:55

+0

@dasblinkenlight我試着將'super'調用移動到頂端,它沒有什麼區別。真的,它感覺像是在黑暗中拍攝的,因爲字符串文字工作得很好:'[dateTimeLabel setText:@「Woof!」]' – Lockjaw 2012-03-09 04:27:04

+0

我想你一定錯過了dateTimeLabel的IBOutlet綁定。檢查它的連接? – 2012-03-09 04:31:24

回答

3

您添加子視圖控制器的方法不正確。嘗試使用下面的代碼(用你的方法,當你調用ViewWillAppear時,認爲視圖控制器的視圖還沒有初始化)(你用簡單的黑客檢查:在移動VC初始化之後加上mobileVC.view;

- (IBAction)setDateTimeButtonClicked:(id)sender { 
     DataObject *theDataObject = [self theAppDataObject]; 

     theDataObject.deadline = [datePicker date]; 

     FirstMobileViewController *mobileVC = [[FirstMobileViewController alloc] init]; 
     [self addChildViewController:mobileVC]; 
     UIView *superView = self.view.superview; 
     mobileVC.view.frame = superView.bounds; 

     [UIView transitionWithView:superview 
          duration:0.5 
          options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionAllowAnimatedContent 
         animations:^{[self.view removeFromSuperview]; 
             [superview addSubView:mobileVC.view]} 
         completion:^(BOOL finished) { 
             [mobileVC didMoveToParentViewController:self]; 
     }]; 
    } 

使用此方法,應該自動調用viewWillAppear。

+0

請注意,提問者的代碼使用'release',意味着他處於非ARC環境中。應該在方法的末尾釋放'mobileVC',或者在創建時使用'alloc/init/autorelease'模式。 – ikuramedia 2012-03-13 09:26:06

0

首先,你在做任何事情之前先發布mobileVC,所以當它運行viewWillAppear:方法中的代碼時,就發生了這一切。

因爲你只有allocinit它你也沒有加載XIB文件mobileVC它。因此,您在viewWillAppear中引用的對象很可能不存在於您明確調用該方法的位置。

您還沒有顯示出用於呈現mobileVC的代碼,因此很難猜測修復程序將會是什麼,或者瞭解您使用字符串文字工作的古怪問題。但是,足以說,當您呈現新的視圖控制器時,它將是與您明確調用的實例不同的實例。

可能的解決辦法:

  1. 使用initWithNibName:bundle:而非純init以確保您的XIB被加載。然後通過mobileVC進入您的過渡完成塊來呈現它?
0

將[超級viewWillAppear:動畫]放在viewWillAppear的開頭。

也許你永遠不會在xib到dateTimeLabel連接dateTimeLabel。

嘗試使用[self。 dateTimeLabel setText:dateStr]

相關問題