iphone - UILabel setText 在 vi​​ewWillAppear 中不起作用

标签 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方法正在触发 - dateStr记录者:NSLog当 super View 再次显示时适本地。但是dateTimeLabel从不更新。显然,评论 NSLog线没有什么区别。

令人抓狂的事情是,即使 NSLog日志dateStr就好了,如果我改变dateStr比如说,@"Yo!"或者甚至是本地初始化的字符串,然后 dateTimeLabel会更新,没问题。

我在这里缺少什么?

最佳答案

您添加 subview Controller 的方法不正确。尝试使用以下代码(使用您的方法,当您调用 ViewWillAppear 时,认为 View Controller 的 View 尚未初始化。(您可以通过简单的 hack 来检查:添加 mobileVC.view; 就在 mobileVC 初始化之后)

    - (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。

关于iphone - UILabel setText 在 vi​​ewWillAppear 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9628866/

相关文章:

iphone - Android 的 viewDidLoad 和 viewDidAppear 等效

iphone - 我如何加载我的 UITableView 以及我的文档目录中保存的文件列表?

ios - 以编程方式获取 Bundle Identifier

适用于德国应用商店的 iPhone 应用(本地化)

iphone - 可以使用 Apple 的 native css 拒绝应用程序吗?

ios - CFBundle 运行时错误 - 这是什么意思?

IOS自定义从左向右滑动UIViewcontroller并导致segue错误

ios - 应该 [UIView endEditing :YES] ever return no?

ios - UICollectionView for Springboard 像文件夹

ios - 从 Project 导入时,Xcode Image Assets Catalog 无法看到所有图像?