的UILabel的setText不viewWillAppear中
工作在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];
}
澄清: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
适当地记录当上海华再次显示。但dateTimeLabel
从未更新。显然,评论NSLog
系列没有什么区别。
的狂揽的事情是,即使NSLog
日志dateStr
就好了,如果我改变dateStr
,比方说,@"Yo!"
甚至到本地初始化字符串,然后dateTimeLabel
将更新,没有问题。
缺少什么我在这里?
您添加子视图控制器的方法不正确。尝试使用下面的代码(用你的方法,当你调用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。
请注意,提问者的代码使用'release',意味着他处于非ARC环境中。应该在方法的末尾释放'mobileVC',或者在创建时使用'alloc/init/autorelease'模式。 – ikuramedia 2012-03-13 09:26:06
首先,你在做任何事情之前先发布mobileVC
,所以当它运行viewWillAppear:
方法中的代码时,就发生了这一切。
因为你只有alloc
和init
它你也没有加载XIB文件mobileVC
它。因此,您在viewWillAppear
中引用的对象很可能不存在于您明确调用该方法的位置。
您还没有显示出用于呈现mobileVC
的代码,因此很难猜测修复程序将会是什么,或者了解您使用字符串文字工作的古怪问题。但是,足以说,当您呈现新的视图控制器时,它将是与您明确调用的实例不同的实例。
可能的解决办法:
- 使用
initWithNibName:bundle:
而非纯init
以确保您的XIB
被加载。然后通过mobileVC
进入您的过渡完成块来呈现它?
将[超级viewWillAppear:动画]放在viewWillAppear的开头。
也许你永远不会在xib到dateTimeLabel连接dateTimeLabel。
尝试使用[self。 dateTimeLabel setText:dateStr]
你确定'[super viewWillAppear:animated];'需要在最后被调用,而不是在实现的开始?尝试将调用移动到实现的第一行,看看它是否有所作为。 – dasblinkenlight 2012-03-09 04:22:55
@dasblinkenlight我试着将'super'调用移动到顶端,它没有什么区别。真的,它感觉像是在黑暗中拍摄的,因为字符串文字工作得很好:'[dateTimeLabel setText:@“Woof!”]' – Lockjaw 2012-03-09 04:27:04
我想你一定错过了dateTimeLabel的IBOutlet绑定。检查它的连接? – 2012-03-09 04:31:24