以编程方式使用UINavigationController设置RootViewController

时间:2015-02-11 15:11:52

标签: ios objective-c

我有一个带有导航控制器和默认RootViewController的程序。如果我没有以编程方式执行任何操作,应用程序将启动并且RootViewController按预期工作,例如下面的故事板表示:

enter image description here

我遇到的问题是合并了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];

另一个扭曲......可能有几个可选的起始视图控制器。

有什么想法吗?

3 个答案:

答案 0 :(得分:5)

  1. 删除UINavigationController
  2. 选择控制器(在我们的例子中为“可选的起始视图控制器”)
  3. 点击Editor >> Embed In >> Navigation Controller
  4. 现在选择Navigation Controller和右侧实用程序区域,选择选项Is Initial View Controller
  5. 如果要动态更改根视图控制器,那么最好以编程方式进行,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

    希望这有帮助

答案 1 :(得分:1)

如果不使用故事板,我们可以在AppDelegate类中以编程方式设置,只需在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()

答案 2 :(得分:0)

初始化后,您无法将新的根视图控制器设置为UINavigationController。您应该根据需要修改viewControllers UINavigationController数组。

如果您希望可选VC为root VC,请创建@[optionalVC]并将其设置为viewControllers的{​​{1}}数组。

相关问题