UINavigationController 更改子视图与另一个

发布于 2024-09-12 04:23:14 字数 339 浏览 7 评论 0原文

我在导航控制器中有以下结构

RootViewController
         |
         |--FirstViewController
         |
         |--SecondViewController

如何直接从 FirstViewController 跳转到 SecondViewController 而不显示 RootViewController。我想在 FirstViewController 的导航栏中添加一个按钮,例如“转到 SecondViewController”。

I have the following structure in Navigation Controller

RootViewController
         |
         |--FirstViewController
         |
         |--SecondViewController

How can I jump directly from FirstViewController to SecondViewController without showing RootViewController. I would like to put a button to the FirstViewController's NavigationBar like "Go to SecondViewController."

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

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

发布评论

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

评论(1

極樂鬼 2024-09-19 04:23:14

在相应按钮的操作方法中,初始化 SecondViewController,然后组装一个由两个元素组成的 NSArray:RootViewController 和新初始化的 SecondViewController(在该顺序,即索引 0 处的 Root 和索引 1 处的 Second)。

然后调用 NavigationController 的 setViewControllers:animated: 方法,并将视图控制器数组作为第一个参数传递。请记住在调用此方法后releaseSecondViewController,或者在初始化时autorelease它以避免内存泄漏。

澄清一下,这将导致 FirstViewController 被 NavigationController 释放。

示例:

- (void) goToSecondViewController
{
    RootViewController *root = [[self.navigationController viewControllers] objectAtIndex:0];
    SecondViewController *second = [[[SecondViewController alloc] init] autorelease];
    NSArray *controllersArray = [NSArray arrayWithObjects: root, second, nil];

    [self.navigationController setViewControllers:controllersArray animated:YES];
}

参考: UINavigationController 类参考

In the appropriate button's action method, initialize the SecondViewController, then assemble an NSArray consisting of two elements: the RootViewController and the newly initialized SecondViewController (in that order, i.e. Root at index 0 and Second at index 1).

Then call the NavigationController's setViewControllers:animated: method, and pass the array of view controllers as the first argument. Remember to release the SecondViewController after calling this method, or autorelease it upon initialization to avoid a memory leak.

Just to clarify, this will cause the FirstViewController to be released by the NavigationController.

Sample:

- (void) goToSecondViewController
{
    RootViewController *root = [[self.navigationController viewControllers] objectAtIndex:0];
    SecondViewController *second = [[[SecondViewController alloc] init] autorelease];
    NSArray *controllersArray = [NSArray arrayWithObjects: root, second, nil];

    [self.navigationController setViewControllers:controllersArray animated:YES];
}

Reference: UINavigationController Class Reference

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