gpt4 book ai didi

ios - didMoveToParentViewController 调用了两次

转载 作者:技术小花猫 更新时间:2023-10-29 10:51:10 25 4
gpt4 key购买 nike

我做了一个简单的测试来了解更多关于包含 View Controller 的信息。
我有一个 View Controller ,我在其中使用两个按钮的操作添加/删除了一个 View Controller :


- (IBAction)myInfoAddAction:(id)sender {
profileViewController = [[ProfileViewController alloc] init];
[self addChildViewController: profileViewController];
UIView *__view = profileViewController.view;
[self.view addSubview: __view];
[profileViewController didMoveToParentViewController: self];
}

- (IBAction)myInfoRemoveAction:(id)sender {
[profileViewController willMoveToParentViewController: nil];
[profileViewController.view removeFromSuperview];
[profileViewController removeFromParentViewController];
}

ProfileViewController 扩展 UIViewController 并覆盖 willMoveToParentViewControllerdidMoveToParentViewController 方法


-( void)willMoveToParentViewController:(UIViewController *)parent
{
[super willMoveToParentViewController: parent];
NSLog(@"willMoveToParentViewController -> %@", self);
}

-( void) didMoveToParentViewController:(UIViewController *)parent
{
[super didMoveToParentViewController: parent];
NSLog(@"didMoveToParentViewController -> %@", self);
}

-( void)viewWillAppear:(BOOL)animated
{
NSLog(@"viewWillAppear -> %@", self);
}

-( void)viewDidDisappear:(BOOL)animated
{
NSLog(@"viewDidDisappear -> %@", self);
}

当我添加 View Controller 时,我得到了以下日志:


willMoveToParentViewController -> <ProfileViewController: 0x8c52ea0>
viewWillAppear -> <ProfileViewController: 0x8c52ea0>
didMoveToParentViewController -> <ProfileViewController: 0x8c52ea0>
didMoveToParentViewController -> <ProfileViewController: 0x8c52ea0>

当我删除 View Controller 时:


willMoveToParentViewController -> <ProfileViewController: 0x8c52ea0>
didMoveToParentViewController -> <ProfileViewController: 0x8c52ea0>
viewDidDisappear -> <ProfileViewController: 0x8c52ea0>
didMoveToParentViewController -> <ProfileViewController: 0x8c52ea0>

我想知道为什么 didMoveToParentViewController 被调用了两次?

谢谢!

最佳答案

我也遇到了同样的问题。此外,我发现当我在 viewDidLoad 期间添加 subview Controller 时,我只有一条消息。我通过删除对 [super didMoveToParentViewController:] 的调用来修复它。我认为问题与在按钮回调中添加 subview Controller 有关,但我不确定为什么这会导致问题。

关于ios - didMoveToParentViewController 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18313888/

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