簡體   English   中英

以編程方式使用UINavigationController設置RootViewController

[英]Setting a RootViewController with UINavigationController Programmatically

我有一個帶有導航控制器和默認RootViewController的程序。 如果我沒有以編程方式執行任何操作,應用程序將啟動並且RootViewController按預期工作,例如下面的故事板表示:

在此輸入圖像描述

我遇到的問題是通過合並Optional Start ViewController。 我想要的是:在我的AppDelegate(didFinishLaunchingWithOptions)中,我想要這樣的代碼:

UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"OptionalStartViewController"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];

首先顯示可選的Start ViewController。 然后,在用戶完成Optional可視控件后,他們可以顯示RootViewController。

所以在Optional Start ViewController中,我添加了這樣的代碼來顯示Root View Controller:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
[self appDelegate].window.rootViewController = viewController;
[[self appDelegate].window makeKeyAndVisible];

這一切都有效, 除了 RootViewController,如圖所示,沒有預期的導航控件(即視圖顯示沒有導航控件)。

我也嘗試過以下代碼(使用UINavigationController而不是ViewController),結果相同......

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
[self appDelegate].window.rootViewController = viewController;
[[self appDelegate].window makeKeyAndVisible];

另一個扭曲......可能有幾個可選的啟動視圖控制器。

有任何想法嗎?

  1. 刪除UINavigationController
  2. 選擇控制器(在我們的例子中為“可選的啟動視圖控制器”)
  3. 單擊Editor >> Embed In >> Navigation Controller
  4. 現在選擇Navigation Controller和右側實用程序區域,選擇選項Is Initial View Controller

如果要動態更改根視圖控制器,那么最好以編程方式執行,在didFinishLaunchingWithOptions ,獲取窗口實例,使用根視圖控制器初始化導航控制器,然后將Windows根視圖控制器設置為導航控制器。 這是代碼。

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

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    OptionalFirstViewController *optionalFirstViewController = [[OptionalFirstViewController alloc] initWithNibName:@"OptionalFirstViewController" bundle:nil];
    UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:optionalFirstViewController];
    [optionalFirstViewController release];
    self.window.rootViewController = homeNavigationController;
    [homeNavigationController release];
    [self.window makeKeyAndVisible];

    return YES;
}

並確保取消選中故事板中所有視圖控制器的Is Initial View Controller

希望這可以幫助

如果不使用storyboard,我們可以在AppDelegate Class中以編程方式設置,只需在AppDelegate記下這些代碼行。

AppDelegate.h

@property (strong, nonatomic) UIStoryboard *storyboard;
@property (strong, nonatomic) UINavigationController *navigationController;

AppDelegate.m

if(self.window == nil)
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

if(self.navigationController == nil)
    self.navigationController = [[UINavigationController alloc]init];

if(self.storyboard == nil)
    self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

self.navigationController.navigationBarHidden = YES;

// Here we can check user is login or not also,

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[self.navigationController pushViewController:ivc animated:YES];
self.window.rootViewController = ivc;


[self.window setRootViewController:self.navigationController];
[self.window makeKeyAndVisible];

Swift版本中

var window: UIWindow?
var storyBoard :UIStoryboard?
var navigationController : UINavigationController?
var mainController :MainViewController = MainViewController()

if window == nil {
        window = UIWindow(frame: UIScreen.main.bounds)
    }
    if navigationController == nil {
        navigationController = UINavigationController()
    }
    if storyBoard == nil {
        storyBoard = UIStoryboard(name: "Main", bundle:nil)
    }
    navigationController?.setNavigationBarHidden(true, animated: true)
    // storyboard with identifer
    mainController = storyBoard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
    navigationController?.pushViewController(mainController , animated: true)

    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

初始化后,無法將新的根視圖控制器設置為UINavigationController 您應該根據需要修改UINavigationController viewControllers數組。

如果您希望可選VC為根VC,請創建@[optionalVC]並將其設置為UINavigationController viewControllers數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM