在 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 在 viewWillAppear 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9628866/