使用故事板以编程方式设置初始视图控制器
如何以编程InitialViewController
为Storyboard设置InitialViewController
? 我想打开我的故事板到一个不同的视图,取决于从发射到发射可能有所不同的一些条件。
如何没有虚拟的初始视图控制器
确保所有初始视图控制器都有一个Storyboard ID。
在故事板中,取消选中第一个视图控制器的“初始视图控制器”属性。
如果你在这个时候运行你的应用程序,你会看到:
Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?
你会注意到,你的应用程序委托的窗口属性现在为零。
在应用程序的设置中,转到目标和“ Info
选项卡。 有明确的Main storyboard file base name
的价值。 在“ General
选项卡上,清除“ Main Interface
的值。 这将删除警告。
在应用程序委托的application:didFinishLaunchingWithOptions:
创建窗口和所需的初始视图控制器application:didFinishLaunchingWithOptions:
方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>]; self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; return YES; }
对于所有的Swift爱好者来说,这里是@Travis翻译成SWIFT的答案:
在ObjectiveC代码之前做什么@Travis解释。 然后,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) var exampleViewController: ExampleViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ExampleController") as! ExampleViewController self.window?.rootViewController = exampleViewController self.window?.makeKeyAndVisible() return true }
ExampleViewController
将是您想要显示的新的初始视图控制器。
解释的步骤:
- 用当前窗口的大小创建一个新窗口,并将其设置为我们的主窗口
- 实例化一个我们可以用来创建新的初始视图控制器的故事板
- 基于它的Storyboard ID实例化我们的新的初始视图控制器
- 将我们的新窗口的根视图控制器设置为我们刚启动的新控制器
- 使我们的新窗口可见
享受和快乐的编程!
你可以在(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
以编程方式设置关键窗口的rootViewController (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
例如:
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (shouldShowAnotherViewControllerAsRoot) { UIStoryboard *storyboard = self.window.rootViewController.storyboard; UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"]; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; } return YES; }
您可以将Navigation rootviewcontroller设置为主视图控制器。 这个想法可以根据应用需求用于自动登录。
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; UIViewController viewController = (HomeController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"HomeController"]; UINavigationController navController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = navController; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { // do stuff for iOS 7 and newer navController.navigationBar.barTintColor = [UIColor colorWithRed:88/255.0 green:164/255.0 blue:73/255.0 alpha:1.0]; navController.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:88/255.0 green:164/255.0 blue:73/255.0 alpha:1.0]; navController.navigationBar.tintColor = [UIColor whiteColor]; navController.navigationItem.titleView.tintColor = [UIColor whiteColor]; NSDictionary *titleAttributes =@{ NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0], NSForegroundColorAttributeName : [UIColor whiteColor] }; navController.navigationBar.titleTextAttributes = titleAttributes; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; } else { // do stuff for older versions than iOS 7 navController.navigationBar.tintColor = [UIColor colorWithRed:88/255.0 green:164/255.0 blue:73/255.0 alpha:1.0]; navController.navigationItem.titleView.tintColor = [UIColor whiteColor]; } [self.window makeKeyAndVisible];
对于StoryboardSegue用户
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; // Go to Login Screen of story board with Identifier name : LoginViewController_Identifier LoginViewController *loginViewController = (LoginViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@“LoginViewController_Identifier”]; navigationController = [[UINavigationController alloc] initWithRootViewController:testViewController]; self.window.rootViewController = navigationController; [self.window makeKeyAndVisible]; // Go To Main screen if you are already Logged In Just check your saving credential here if([SavedpreferenceForLogin] > 0){ [loginViewController performSegueWithIdentifier:@"mainview_action" sender:nil]; }
谢谢
打开mainstoryboard,选择你想要首先启动的视图,然后打开实用工具 – >属性。 在“视图控制器”下方,您会看到“初始视图控制器”单选按钮。 只要选择它。
—修改的问题:
也许你可以试试这个方法:在你的初始视图的ViewDidLoad部分写一个方法,当方法运行在应用程序启动时,方法触发一个segue到另一个视图。
Swift 3:更新到@ victor-sigler的代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) // Assuming your storyboard is named "Main" let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) // Add code here (eg if/else) to determine which view controller class (chooseViewControllerA or chooseViewControllerB) and storyboard ID (chooseStoryboardA or chooseStoryboardB) to send the user to if(condition){ let initialViewController: chooseViewControllerA = mainStoryboard.instantiateViewController(withIdentifier: "chooseStoryboardA") as! chooseViewControllerA self.window?.rootViewController = initialViewController ) }else{ let initialViewController: chooseViewControllerB = mainStoryboard.instantiateViewController(withIdentifier: "chooseStoryboardB") as! chooseViewControllerB self.window?.rootViewController = initialViewController ) self.window?.makeKeyAndVisible( return true }
我不认为这是可能的。 相反,你可以有一个初始的控制器,将会有不同的视图控制器。 在启动时,您可以决定以编程方式执行哪个segue。
您可以使用Interface Builder以编程方式设置initial view controller
。
下面是以编程方式使用的方法。
Objective-C:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; // <storyboard id> self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; return YES;
Swift:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) var objMainViewController: MainViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainController") as! MainViewController self.window?.rootViewController = objMainViewController self.window?.makeKeyAndVisible() return true
我创建了一个路由类来处理动态导航,并保持清洁的AppDelegate类,我希望它也会帮助其他的。
// // Routing.swift // // // Created by Varun Naharia on 02/02/17. // Copyright © 2017 TechNaharia. All rights reserved. // import Foundation import UIKit import CoreLocation class Routing { class func decideInitialViewController(window:UIWindow){ let userDefaults = UserDefaults.standard if((Routing.getUserDefault("isFirstRun")) == nil) { Routing.setAnimatedAsInitialViewContoller(window: window) } else if((userDefaults.object(forKey: "User")) != nil) { Routing.setHomeAsInitialViewContoller(window: window) } else { Routing.setLoginAsInitialViewContoller(window: window) } } class func setAnimatedAsInitialViewContoller(window:UIWindow) { Routing.setUserDefault("Yes", KeyToSave: "isFirstRun") let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let animatedViewController: AnimatedViewController = mainStoryboard.instantiateViewController(withIdentifier: "AnimatedViewController") as! AnimatedViewController window.rootViewController = animatedViewController window.makeKeyAndVisible() } class func setHomeAsInitialViewContoller(window:UIWindow) { let userDefaults = UserDefaults.standard let decoded = userDefaults.object(forKey: "User") as! Data User.currentUser = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! User if(User.currentUser.userId != nil && User.currentUser.userId != "") { let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let homeViewController: HomeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController let loginViewController: UINavigationController = mainStoryboard.instantiateViewController(withIdentifier: "LoginNavigationViewController") as! UINavigationController loginViewController.viewControllers.append(homeViewController) window.rootViewController = loginViewController } window.makeKeyAndVisible() } class func setLoginAsInitialViewContoller(window:UIWindow) { let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let loginViewController: UINavigationController = mainStoryboard.instantiateViewController(withIdentifier: "LoginNavigationViewController") as! UINavigationController window.rootViewController = loginViewController window.makeKeyAndVisible() } class func setUserDefault(_ ObjectToSave : Any? , KeyToSave : String) { let defaults = UserDefaults.standard if (ObjectToSave != nil) { defaults.set(ObjectToSave, forKey: KeyToSave) } UserDefaults.standard.synchronize() } class func getUserDefault(_ KeyToReturnValye : String) -> Any? { let defaults = UserDefaults.standard if let name = defaults.value(forKey: KeyToReturnValye) { return name as Any } return nil } class func removetUserDefault(_ KeyToRemove : String) { let defaults = UserDefaults.standard defaults.removeObject(forKey: KeyToRemove) UserDefaults.standard.synchronize() } }
并在你的AppDelegate调用这个
self.window = UIWindow(frame: UIScreen.main.bounds) Routing.decideInitialViewController(window: self.window!)
使用Swift 3和Swift 4避免强制转型的另一个解决方案就是这样
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) guard let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewController") as? YourViewController else { return false } self.window?.rootViewController = viewController self.window?.makeKeyAndVisible() return true }
下面是使用UINavigationController
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) guard let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewController") as? YourViewController else { return false } let navigationController = UINavigationController(rootViewController: viewController) self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible() return true }
前几天我遇到了同样的情况。 一个非常简单的技巧解决了这个问题 我在launch2之前隐藏了我的初始视图控制器。 如果初始视图控制器是正确的控制器,它被设置为在viewDidLoad中可见。 否则,执行到所需的视图控制器。 它适用于iOS 6.1及以上版本。 我相信它可以在早期版本的iOS上运行。
感谢您在AppDelegate中对此进行如下修改:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //Some code to check value of pins if pins! == "Verified"{ print(pins) self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "HomePage", bundle: nil) let exampleViewController: UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("SBHP") as! UINavigationController self.window?.rootViewController = exampleViewController self.window?.makeKeyAndVisible() }else{ print(pins) self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let exampleViewController: UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("SBUser") as! UINavigationController self.window?.rootViewController = exampleViewController self.window?.makeKeyAndVisible() }
找到简单的解决方案 – 无需从故事板中删除“初始视图控制器检查”和编辑项目信息选项卡,并使用makeKeyAndVisible
,只是地方
self.window.rootViewController = rootVC;
在
- (BOOL) application:didFinishLaunchingWithOptions:
在AppDelegate.swift
您可以添加以下代码:
let sb = UIStoryboard(name: "Main", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "YourViewController") self.window?.rootViewController = vc self.window?.makeKeyAndVisible()
当然,你需要实现你的逻辑,根据哪个标准你会选择一个合适的视图控制器。
另外,不要忘记添加一个身份。
选择您想要打开的视图控制器,然后转到属性检查器。 转到初始场景,并检查是初始视图控制器选项。
现在这将是您的初始视图控制器,将在应用程序启动时首先打开。
- IOS模拟器与小牛慢
- 如何在UIView下绘制阴影?
- 如何使图像和标签自定义UIBarButtonItem?
- 我在哪里可以find一个CSV到Objective-C的NSArrayparsing器?
- 未find框架GoogleToolboxForMac
- Swift 2.0 – 二元运算符“|”不能应用于两个UIUserNotificationType操作数
- NSDefaultRunLoopMode与NSRunLoopCommonModes
- 目标C中的简单string连接
- Cocoapods警告 – CocoaPods没有设置你的项目的基本configuration,因为你的项目已经有一个自定义的configuration集