如何创建导航控制器以将主视图控制器连接到 2 个自定义视图控制器

发布于 2024-12-02 17:46:47 字数 308 浏览 0 评论 0原文

我需要一些有关如何创建将其根视图控制器加载到主视图控制器的导航控制器的帮助。然后,我想让导航控制器有两个选择器按钮(leftBarButtonItem 和 rightBarButtonItem),将用户带到 2 个自定义视图控制器(AboutViewController 和 SettingsViewController)。

我成功地做到了这一点,但相反,我在 MainWindow.xib 中创建了 2 个视图控制器,并将所有代码放在 AppDelegate.h 和 AppDelegate.m 中。这显然不是一个好的做法,尤其是当您有很多自定义视图控制器时。

谢谢。

I need some help on how to create a Navigation Controller that loads its Root View Controller to a Main View Controller. Then, I want to have that Navigation Controller two selector buttons (leftBarButtonItem and rightBarButtonItem) that will take the user to 2 custom View Controllers (AboutViewController and SettingsViewController).

I was able to do this successfully but instead, I created the 2 view controllers inside the MainWindow.xib with all codes inside AppDelegate.h and AppDelegate.m. This is obviously not a good practice especially when you have a lot of custom view controllers.

Thanks.

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

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

发布评论

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

评论(1

孤单情人 2024-12-09 17:46:47

你是对的,在 AppDelegate 中创建视图控制器并不是一个好的做法。相反,您应该为每个视图创建一个单独的 ViewController。在 rootViewController 的实现文件中,创建方法来推送要在按下按钮时显示的单独视图控制器。像这样的事情会起作用:

- (void)showAboutView
{
    AboutViewController *aboutViewController = [[AboutViewController alloc] init];
    [self.navigationController pushViewController:aboutViewController animated:YES];
}

- (void)showSettingsView
{
    SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
    [self.navigationController pushViewController:settingsViewController animated:YES];
}

然后,将这些方法包含在按钮的选择器字段中。像这样:

UIBarButtonItem *aboutBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAboutView)];

[[self navigationItem] setRightBarButtonItem:aboutBarButtonItem];

这应该能够推动你的观点。导航控制器将自动显示一个“返回”按钮。

在 AppDelegate 中,您将创建 navigationController 并告诉它哪个视图将成为 rootViewController,如下所示:

ViewController1 *vc1 = [[ViewController1 alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc1];

希望这会有所帮助!

You are right that it isn't good practice to create your view controllers in the AppDelegate. Instead, you should create a separate ViewController for each view. In the implementation file for your rootViewController, create methods to push the separate view controllers that you want to show when the buttons are pressed. Something like this would work:

- (void)showAboutView
{
    AboutViewController *aboutViewController = [[AboutViewController alloc] init];
    [self.navigationController pushViewController:aboutViewController animated:YES];
}

- (void)showSettingsView
{
    SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
    [self.navigationController pushViewController:settingsViewController animated:YES];
}

Then, include those methods in the selector field for your buttons. Like this:

UIBarButtonItem *aboutBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAboutView)];

[[self navigationItem] setRightBarButtonItem:aboutBarButtonItem];

This should take care of pushing your view. The navigationController will automatically show a button to 'go back'.

In the AppDelegate, you would create the navigationController and tell it what view will be the rootViewController, like this:

ViewController1 *vc1 = [[ViewController1 alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc1];

Hope this helps!

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