gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:58:42 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com