gpt4 book ai didi

ios - 如何从 super super View 中删除自身和 super View ?

转载 作者:行者123 更新时间:2023-11-29 04:54:08 24 4
gpt4 key购买 nike

我有一个 UIViewControllerABA 使用以下方式加载 B:[A.view addSubView B.view]

B 有一个带有“后退”按钮的导航栏。我想在单击时返回到 A,因此在选择器中我尝试了 [self.view removeFromSuperview],但它只删除了导航栏。然后我尝试了[self.view.superview removeFromSuperview],它仍然只是删除了导航栏。我该怎么办?

此外,“后退”按钮的另一个小问题:设置其标题。我尝试了这两种方式,但仍然显示“返回”。

navItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Chapter" style:UIBarButtonItemStylePlain target:self action:@selector(handleBackBarButtonItem:)];

navItem.backBarButtonItem.title = @"Chapter";

提前谢谢您!

最佳答案

我认为您不太了解导航(使用 UINavigationController)在 iOS 中的工作原理。假设您想要动画,这就是您想要的:

  1. 设置 UINavigationController。这可以在应用程序的委托(delegate)中完成(为了避免内存泄漏,请在 UINavigationController *navController 上设置一个实例变量:

    navController = [[UINavigationController alloc] initWithRootViewController:A];

    请注意,我们添加 A 作为我们的 Root View Controller 。

  2. 需要时按下第二个 View Controller 。我假设您在单击按钮或其他操作后添加 B.view 。在添加第二个 View Controller 的方法的实现中,运行以下代码,而不是[A.view addSubview:B.view]。此方法应位于第一个 Controller 的 .m 文件中:

    [self.navigationController pushViewController:B animated:YES];

    这也会产生很好的过渡效果。

  3. 将第二个 View Controller 从堆栈中弹出。使用UINavigationController,一个漂亮的箭头形状的后退按钮会自动包含在推送的 View Controller 中,以导航回最后一个 View Controller 。这意味着您甚至不需要任何代码来允许向后导航。

就是这样!现在,如果您需要更改 B 的后退按钮的标题,请在 AviewDidLoad 方法中执行此操作:

self.navigationItem.backBarButtonItem = customBackButtonItem;

关于ios - 如何从 super super View 中删除自身和 super View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8319640/

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