浏览 UIViewControllers/UIView—代码和概念问题

发布于 2024-12-01 10:45:23 字数 1284 浏览 2 评论 0原文

下面有代码,但这也是一个概念问题。问题:下面的方法是在视图控制器中移动的正确方法吗?这很宽泛,所以让我添加一些具体细节。

假设您位于视图 A 中。该视图提供了转到视图 B 或视图 C 的选项。用户做出选择后 (i) 我想要加载新视图,并且 (ii) 用户将不会返回到视图 A。(顺便说一句,视图 A 是我最初的 rootViewController。)

因此,假设我在视图 A 中连接了两个按钮,每个按钮都加载其各自的视图。这是一种有效/可靠的方法吗:

- (IBAction)loadViewB:(id)sender
{
    ViewBController *viewBController = [[ViewBController alloc] initWithNibName:Nil bundle:Nil];
    [self.view.superview insertSubview:viewBController.view atIndex:0];
    [self.view removeFromSuperview];
}   


- (IBAction)loadViewC:(id)sender
{     
    ViewCController *viewCController = [[ViewCController alloc] initWithNibName:Nil bundle:Nil];
    [self.view.superview insertSubview:viewCController.view atIndex:0];
    [self.view removeFromSuperview];
}

“insertSubview:atIndex:”方法似乎是正确的,基于 Apple 的视图控制器编程指南。此外,该代码有效。但我应该调用“removeFromSuperview”吗?或者我应该把它们堆起来? (如果有人对内存管理有意见,我会洗耳恭听。)

另外:

  1. 我不想浏览结构化数据。因此,使用 UINavigationController 似乎并不正确。
  2. 我不想暂时中断流程。因此,采用模态视图。

我知道这是一个简单的问题,但我只是想确保我正确理解了文档并总体掌握了这个概念。

提前致谢。

There's code below, but this is also a conceptual issue. The question: Are the methods below the right way to move through view controllers? And that's broad, so let me add some specific details.

Say you're in View A. This view presents options to go to View B or View C. After the user makes the choice (i) I want to load the new view and (ii) the user won't be going back to View A. (Also, btw, View A was my initial rootViewController.)

So, say I wire up two buttons in View A, each to load its respective view. Is this an efficient/solid way to do it:

- (IBAction)loadViewB:(id)sender
{
    ViewBController *viewBController = [[ViewBController alloc] initWithNibName:Nil bundle:Nil];
    [self.view.superview insertSubview:viewBController.view atIndex:0];
    [self.view removeFromSuperview];
}   


- (IBAction)loadViewC:(id)sender
{     
    ViewCController *viewCController = [[ViewCController alloc] initWithNibName:Nil bundle:Nil];
    [self.view.superview insertSubview:viewCController.view atIndex:0];
    [self.view removeFromSuperview];
}

The "insertSubview:atIndex:" method seems to be right, based on Apple's View Controller Programming Guide. Also, the code works. But should I call the "removeFromSuperview"? Or should I just stack them up? (And if anyone has comments about memory management, I'm all ears.)

Also:

  1. I don't want to move through structured data. So, it doesn't seem like using a UINavigationController would be right.
  2. I don't want to temporarily interrupt flow. So, strike a modal view.

I know it's a simple question, but I just want to make sure I've understood the documentation right and have grasped the concept generally.

Thanks ahead of time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2024-12-08 10:45:23

我不认为你的做法有什么问题。请注意,简单的 [self.view removeFromSuperview] 可能不会导致释放任何内存,例如,如果您的应用程序委托持有对根视图控制器的引用。将视图切换责任交给应用程序委托可能是一个好主意,这样它就可以根据需要选择释放对根视图控制器的引用。

I don't think there's anything wrong with your approach. Just beware that simply [self.view removeFromSuperview] might not cause any memory to be freed up, for example if your app delegate holds a reference to your root view controller. It might be a good idea to give this view-switching responsibility to the app delegate, so it can choose to release the reference to the root view controller if it desires.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文