如何替换“基于导航的应用程序”中的RootViewController

发布于 2024-11-04 00:50:45 字数 316 浏览 2 评论 0原文

我有一个在 XCode 中使用“基于导航的应用程序”模板的应用程序。

现在我想更改它,以便加载的第一个视图是常规(自定义)UIView,如果用户单击特定按钮,我会将原始 RootViewController 推送到 NavigationController 上。

我知道在某个地方,有人用我的 RootViewController 调用它:

- (id)initWithRootViewController:(UIViewController *)rootViewController

我想知道如何用我的新类替换参数。

I have an application that uses the "navigation-based application" template in XCode.

Now I want to change it so that the first view that loads is a regular (custom) UIView, and if the user clicks a particular button, I push the original RootViewController onto the NavigationController.

I understand that somewhere, someone is calling this with my RootViewController:

- (id)initWithRootViewController:(UIViewController *)rootViewController

I want to know how to replace the argument with my new class.

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

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

发布评论

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

评论(3

那片花海 2024-11-11 00:50:45

如果您想替换导航堆栈的根视图控制器,您可以将其视图控制器数组的第一个对象替换为 -

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];

NewViewController *nvc = [[NewViewController alloc] init];
[viewControllers replaceObjectAtIndex:0 withObject:nvc];
[self.navigationController setViewControllers:viewControllers];

if you want to replace the root view controller of your navigation stack you can replace the first object of its view controllers array as -

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];

NewViewController *nvc = [[NewViewController alloc] init];
[viewControllers replaceObjectAtIndex:0 withObject:nvc];
[self.navigationController setViewControllers:viewControllers];
薯片软お妹 2024-11-11 00:50:45

^ 这些都是以编程方式完成的方法。这很酷。但我在 Xcode 中使用界面生成器和故事板,因此这是添加新视图控制器的简单快捷方法:

  • 打开有问题的故事板
  • 通过从对象列表中拖动新的视图控制器(右手工具)将其添加到故事板栏底部)
  • 按住 CONTROL 键的同时,单击导航控制器的中间(应为空白和灰色)并将其拖动到新的白色视图。
  • 在弹出窗口中,选择 Relation Segue: Root View Controller(应位于您之前可能见过的正常推送/模式/自定义选项下方)

Tada!享受新的根视图控制器,而无需因编程创建而耽误您的一天。

^ These are all ways to do it programmatically. Thats cool. But I use the interface builder and storyboards in Xcode, so this is the easy and fast way to add a new view controller:

  • Open the storyboard in question
  • Add a new view controller to your storyboard by dragging it from the objects list (right hand tool bar bottom)
  • While holding down the CONTROL key, click and drag from the middle of your navigation controller (should be blank and gray) to your new fresh white view.
  • On the popup, selection Relation Segue: Root View Controller (should be below the normal push/modal/custom options you have likely seen before)

Tada! Enjoy your new root view controller without holding your day up with programmatic creation.

慈悲佛祖 2024-11-11 00:50:45

查看主应用程序委托 .m 文件并找到方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

里面会有这样一行

self .window.rootViewController = self.navigationController;

您可以在那里实例化不同的视图控制器并将其指定为 rootViewController

Look inside the main app delegate .m file and find the method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Inside it will be a line like this

self.window.rootViewController = self.navigationController;

You can instantiate a diffent view controller there and assign it to be the rootViewController

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