UILabel setText在viewWillAppear中不起作用

时间:2012-03-09 04:17:55

标签: iphone objective-c cocoa-touch cocoa ios5

ViewController我有以下内容:

- (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];
}

澄清:dateTimeLabel IS 已连接到xib文件中。 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行进行评论并没有什么不同。

MADDENING 是即使dateStr记录dateStr就好了,如果我将@"Yo!"更改为,例如, dateTimeLabel甚至是本地初始化的字符串,然后{{1}}会更新,没问题。

我在这里缺少什么?

3 个答案:

答案 0 :(得分:3)

添加子视图控制器的方法不正确。尝试使用以下代码(使用您的方法,当您调用ViewWillAppear时,请认为视图控制器的视图尚未初始化。(您通过简单的黑客检查:在mobileVC初始化之后添加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。

答案 1 :(得分:0)

首先,你在使用它之前发布mobileVC,所以当它运行viewWillAppear:方法中的代码时,就会发生这一切。

您还没有加载mobileVC的XIB文件,因为您只有allocinit。因此,您在viewWillAppear中引用的对象很可能在您明确调用该方法时不存在。

您没有显示用于呈现mobileVC的代码,因此很难猜测修复将会是什么,或者了解您在使用字符串文字时遇到的奇怪问题。但是,足以说明,当您呈现新的视图控制器时,它将与您明确调用的实例不同。

可能的解决方法:

  1. 使用initWithNibName:bundle:代替普通init,以确保您的XIB已加载。然后将mobileVC传递到转换的完成块以显示它?

答案 2 :(得分:0)

将[super viewWillAppear:animated]放在viewWillAppear的开头。

也许你永远不会将xib中的dateTimeLabel连接到dateTimeLabel。

尝试使用[self。 dateTimeLabel setText:dateStr]

相关问题