popviewcontroller 没有调用 viewWillappear
当用户单击按钮时,我使用以下代码显示上一个视图。
[self.navigationController popViewControllerAnimated:YES];
在上一个视图中,我覆盖 viewWillAppear 以初始化一些内容。但是,似乎 viewWillAppear 没有被调用。我将 NSLog 放入 viewDidload、viewWillAppear、viewDidAppear 中,并且仅调用 viewDidAppear。这是正常行为吗?如果是,我应该重写什么事件才能进行初始化?谢谢。
根据前一个视图的 -viewWillAppear 请求,
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"ViewWillAppear");
//[[GameStore defaultStore] resetGame];
[self setHangmanImage];
NSLog([[[GameStore defaultStore] selectedList] label]);
[labelListName setText:[NSString stringWithFormat:@"List Name: %@", [[[GameStore defaultStore] selectedList] label]]];
[labelCurrentIndex setHidden:YES];
[labelCurrentWord setHidden:YES];
[[self navigationController] setNavigationBarHidden:NO];
[FlurryAnalytics logEvent:@"GameViewController - viewWillAppear"];
[self getNewQuestion];
NSLog(@"ViewWillAppear finish");
[super viewWillAppear:YES];
}
我使用以下代码在应用程序委托中设置了 UINavigationalController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
HomeViewController *hv = [[HomeViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:hv];
// You can now release the itemsViewController here,
// UINavigationController will retain it
[hv release];
// Place navigation controller's view in the window hierarchy
[[self window] setRootViewController:navController];
[navController release];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
更新
我不知道发生了什么,但昨晚在尝试在模拟器中再次运行应用程序后而且仍然存在这个问题,我决定保存所有内容并关闭计算机,因为时间已经很晚了。
今天早上,我重新打开计算机,打开 xcode,清理项目并构建并运行它,问题得到解决,并且 -viewWillAppear 被调用。我没有改变任何东西并且它正在工作。我在 -willShowView 中添加了 NSLog,但它没有被调用。我不知道为什么突然调用 viewWillAppear 。
I'm using the following code to display the previous view when a user is clicking on a button
[self.navigationController popViewControllerAnimated:YES];
In the previous view, I overwrite viewWillAppear to initialized few things. However, it seems like viewWillAppear is not being called. I put NSLog in viewDidload, viewWillAppear, viewDidAppear and only viewDidAppear is being called. Is this normal behavior? If yes, what event should I override so I can do my initialization? Thank you.
As requested -viewWillAppear for the previous view
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"ViewWillAppear");
//[[GameStore defaultStore] resetGame];
[self setHangmanImage];
NSLog([[[GameStore defaultStore] selectedList] label]);
[labelListName setText:[NSString stringWithFormat:@"List Name: %@", [[[GameStore defaultStore] selectedList] label]]];
[labelCurrentIndex setHidden:YES];
[labelCurrentWord setHidden:YES];
[[self navigationController] setNavigationBarHidden:NO];
[FlurryAnalytics logEvent:@"GameViewController - viewWillAppear"];
[self getNewQuestion];
NSLog(@"ViewWillAppear finish");
[super viewWillAppear:YES];
}
I setup the UINavigationalController in the app delegate using the following code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
HomeViewController *hv = [[HomeViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:hv];
// You can now release the itemsViewController here,
// UINavigationController will retain it
[hv release];
// Place navigation controller's view in the window hierarchy
[[self window] setRootViewController:navController];
[navController release];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
UPDATE
I don't know what happened but last night after trying to run the app one more time in the simulator and its still having this issue, I decided to save everything and shut my computer down since it was getting late.
This morning I turned my computer back on opened up xcode, clean the project and build and run it and I the problem is fixed and -viewWillAppear is called. I didn't change anything and its working. I added NSLog in -willShowView and its not getting called. I don't know why all of a sudden viewWillAppear is being called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保导航控制器的委托已设置,然后使用此函数在要调用其 viewWillAppear 的类中调用 viewWillAppear:
Make sure your navigation controller's delegate is set and then use this function to call viewWillAppear in the class whose viewWillAppear you want to call:
我刚刚遇到了一个非常相似的问题,经过一些测试发现,在块中调用 popViewControllerAnimated: (来自 AFNetworking 中的网络响应) viewDidAppear 不会在父视图中调用。
这里对我有用的解决方案是在主线程中调用它。
I've just hit a problem very much the same and after some testing discovered that calling popViewControllerAnimated: in a block (from a network response in AFNetworking) viewDidAppear isn't called in the parent view.
The solution that worked for me here was to call it in the main thread instead.
我刚刚也遇到了这个问题,这个问题的根本原因是因为我把 self.navigationController.delegate = self 放在 viewDidLoad 上。我的解决方案是将委托放在 viewWillAppear 上:
之后 popViewController 将命中 viewWillAppear。
I have just hit this problem as well the root cause of this problem is because I put self.navigationController.delegate = self on viewDidLoad. My solution is to put the delegation on viewWillAppear :
After that popViewController will hit viewWillAppear.