ios - 在 iOS 中使用带有 .xib 文件的多个 View Controller 时尝试使用 UNavigationController 的子类

标签 ios uiviewcontroller uinavigationcontroller

我正在构建一个 iOS 应用程序,其中有多个由 .xib 文件制作的 View Controller 。整个应用程序将允许纵向和横向,但前三个 View Controller 将仅是纵向的。之后的屏幕将是横向的。为此,我创建了 UINavigationController 的两个子类,一个用于在纵向模式下限制前三个,在横向模式下限制最后一个。

目前,我可以显示第一个 View Controller ,并将其锁定为纵向模式。这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    ViewControllerA *testViewA = [[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];
    PortraitNavigationController *navController = [[PortraitNavigationController alloc] initWithRootViewController:testViewA];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = navController;

    [self.window makeKeyAndVisible];

    return YES;
}

PortraitNavigationController(UINavigationController 的子类)的代码包含一个方法,如下所示:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

在 ViewControllerA 中,我有一个按钮,当按下它时,它会调用 ViewControllerB(它也将被锁定在纵向模式下。我在按钮操作方法中的代码如下:

- (IBAction)buttonAction:(id)sender {

   PortraitBViewController *testBView = [[PortraitBViewController alloc] initWithNibName:@"PortraitBViewController" bundle:nil];
        PortraitNavigationController *portraitNav = [[PortraitNavigationController alloc] initWithRootViewController:testBView];

//How do I push this new view Controller on to the stack?

}

正如您在我上面的评论中看到的那样,我如何将下一个 View Controller 推送到此堆栈(当然,然后弹出以返回到前一个 View Controller )?我遇到的问题是,就像传统的应用程序一样,我想要一堆 View Controller ,除了在我的例子中我想使用自定义 UINavigationController 类。

通常,我会使用引用 self.navigationController 将下一个 View Controller 插入堆栈,但在这种情况下,因为我使用的是自定义导航 Controller ,所以看起来我必须创建一个新的引用到我正在使用的每个 View Controller 中的自定义导航 Controller 。如果是这种情况,那么这意味着我每次都在创建一个新的 rootViewController,这基本上意味着我将拥有三个不同的堆栈(每个堆栈一个 View Controller ),而不是三个 View Controller 堆栈。

如何使用对自定义 UINavigationController 的相同引用来调用每个 View Controller 的推送/弹出?最后,我如何将这些 View Controller 连接到另一个使用不同导航 Controller 的 View Controller ,同时将所有内容保持在同一个堆栈中?

最佳答案

如果你使用多个 UINavigationControllers,你的生活将会非常困难。您应该指定 UIViewController 支持哪些方向,而 UINavigationController 不支持。

关于ios - 在 iOS 中使用带有 .xib 文件的多个 View Controller 时尝试使用 UNavigationController 的子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26720859/

相关文章:

ios - Cordova 无法添加插件 : Error: Cannot find module 'unorm'

ios - 今天使用 SpriteKit 的扩展

iOS presentViewController 无法正常工作

ios - 检测表在 iOS 13 上被取消

ios - Root View Controller 的确切含义

objective-c - UINavigationbar 在旋转时丢失设置图像

iphone - iOS "CABasicAnimation"动画完成后将组件带到原始位置

iphone - 核心数据合并行为

ios - 在 iOS 7 中维护导航动画

ios - 完全禁用导航 Controller