用于触发另一个 VC 的按钮,模拟器中未显示导航控制器后退按钮

Button to trigger another VC, Navigation Controller Back Button not showing in Simulator

我有 3 个 VC。 1. Root VC、注册 VC 和登录 VC。

首先,我创建了按钮并通过显示(例如推送)将它们链接到所需的 VC。然后,在根目录 VC,我单击了嵌入在导航控制器中。所有后退按钮都显示在正确的 VC 处,如下图所示。

但是我运行模拟器的时候,后退按钮都不见了。

我已经尝试命名所有的 segues 但它不起作用。

不知道哪里做错了,请大家帮忙

News.m InitView 控制器

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

if(![(AppDelegate*)[[UIApplication sharedApplication] delegate] authenticated]) {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    RootViewController *initView =  (RootViewController*)[storyboard instantiateViewControllerWithIdentifier:@"initialView"];
    [initView setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:initView animated:NO completion:nil];
} else{
    // proceed with the profile view
  }
 }

RootViewController.m

#import "RootViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize loginView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

 - (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}
  - (IBAction)btnLogin:(id)sender {

  [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(loginActionFinished:)
                                               name:@"loginActionFinished"
                                           object:loginView];

  }


 #pragma mark - Dismissing Delegate Methods

 -(void) loginActionFinished:(NSNotification*)notification {

AppDelegate *authObj = (AppDelegate*)[[UIApplication sharedApplication] delegate];
authObj.authenticated = YES;

[self dismissLoginAndShowProfile];
}

- (void)dismissLoginAndShowProfile {
  [self dismissViewControllerAnimated:NO completion:^{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tabView = [storyboard instantiateViewControllerWithIdentifier:@"profileView"];
    [self presentViewController:tabView animated:YES completion:nil];
}];


}

@end

编辑

根据评论中的讨论,解决方案如下所示:

创建 UINavigationController 的子类,比如 RootNavigationController

class RootNavigationController: UINavigationController {
    ....
}

现在,如果用户未注册,通过在 AppDelegate 中设置 rootviewcontroller 将其路由到 RootNavigationController:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootNavigationControllerID")

self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()

在屏幕截图中,RootViewController 是您的 Root VC。

原答案

从您的问题和屏幕截图来看,您的导航控制器似乎不是您的根视图控制器。

  1. 您可以将 NavigationController 设置为情节提要中的初始视图控制器,如下所示:

  1. 如果您从代码中设置了 RootViewController,您可以像这样从 AppDelegate 中设置它:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstNavigationController")
    
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()