在appdelegate中设置初始viewcontroller – swift
我想从appdelegate设置初始viewcontroller。 我发现了一个非常好的答案,但是它在Objective C中,而且我很快在实现同样的事情时遇到了麻烦。
使用故事板以编程方式设置初始视图控制器
- (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; }
任何人都可以帮忙?
我希望最初的Viewcontroller依赖于使用条件语句来满足某些条件。
我用这个线程来帮助我把目标C转换成swift,并且它的工作完美。
在Swift中实例化和呈现一个viewController
Swift 2代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC") self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() return true }
Swift 3代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC") self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() return true }
尝试这个。 例如:您应该使用UINavigationController
作为初始视图控制器。 然后,你可以设置任何视图控制器作为从故事板的根。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController navigationController.viewControllers = [rootViewController] self.window?.rootViewController = navigationController return true }
看我的故事板屏幕。
如果你不使用故事板,你可以试试这个
var window: UIWindow? var initialViewController :UIViewController? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { initialViewController = MainViewController(nibName:"MainViewController",bundle:nil) let frame = UIScreen.mainScreen().bounds window = UIWindow(frame: frame) window!.rootViewController = initialViewController window!.makeKeyAndVisible() return true }
这是一个很好的方法来处理它。 这个例子把一个导航控制器作为根视图控制器,并把你select的视图控制器放在导航堆栈的底部,随时准备向你推送任何你需要的东西。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // mainStoryboard let mainStoryboard = UIStoryboard(name: "MainStoryboard", bundle: nil) // rootViewController let rootViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainViewController") as? UIViewController // navigationController let navigationController = UINavigationController(rootViewController: rootViewController!) navigationController.navigationBarHidden = true // or not, your choice. // self.window self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.rootViewController = navigationController self.window!.makeKeyAndVisible() }
为了使这个例子工作,你可以在主视图控制器上设置“MainViewController”作为Storyboard ID,在这种情况下Storyboard的文件名是“MainStoryboard.storyboard”。 我以这种方式重新命名我的故事板,因为Main.storyboard对我来说不是一个合适的名字,特别是如果你去了它的子类。
我已经完成了目标-c希望它对你有用
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *viewController; NSUserDefaults *loginUserDefaults = [NSUserDefaults standardUserDefaults]; NSString *check=[loginUserDefaults objectForKey:@"Checklog"]; if ([check isEqualToString:@"login"]) { viewController = [storyboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"]; } else { viewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"]; } self.window.rootViewController = viewController; [self.window makeKeyAndVisible];
对于Xcode 8,Swift 3:
从故事板实例化根视图控制器:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // this line is important self.window = UIWindow(frame: UIScreen.main.bounds) // In project directory storyboard looks like Main.storyboard, // you should use only part before ".storyboard" as it's name, // so in this example name is "Main". let storyboard = UIStoryboard.init(name: "Main", bundle: nil) // controller identifier sets up in storyboard utilities // panel (on the right), it called Storyboard ID let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController self.window?.rootViewController = viewController self.window?.makeKeyAndVisible() return true }
如果你想以root身份使用UINavigationController
:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // this line is important self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard.init(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController let navigationController = UINavigationController.init(rootViewController: viewController) self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible() return true }
从xib实例化根视图控制器:
这几乎是一样的,而不是线条
let storyboard = UIStoryboard.init(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController
你必须写
let viewController = YourViewController(nibName: "YourViewController", bundle: nil)
我已经在Xcode 8和Swift 3.0中完成了,希望它对你有用,而且它的工作很完美。 使用以下代码:
var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController") self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() return true }
如果您使用的是导航控制器,请使用以下代码:
var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController let initialViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") navigationController.viewControllers = [initialViewController] self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible() return true }
那么上面/下面的所有答案都会产生一个关于故事板中没有入口点的警告。
如果你想有2(或更多)条目视图控制器依赖于某些条件(如conditionVariable ),那么你应该做的是:
- 在你的Main.storyboard中创build没有 rootViewController的UINavigationController,将其设置为入口点
- 创build2(或更多)“显示”塞格到视图控制器,分配给他们一些id,比如id1和id2
-
使用下一个代码:
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let navigationController = window!.rootViewController! as! UINavigationController navigationController.performSegueWithIdentifier(conditionVariable ? "id1" : "id2") return true }
希望这可以帮助。
以防万一你想在视图控制器而不是在应用程序委托中执行它:只需在视图控制器中获取对AppDelegate的引用,并使用右视图控制器重置它的窗口对象,因为它是rootviewController。
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("YOUR_VC_IDENTIFIER") as! YourViewController appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible()
I worked out a solution on Xcode 6.4 in swift. // I saved the credentials on a click event to phone memory @IBAction func gotobidderpage(sender: AnyObject) { if (usernamestring == "bidder" && passwordstring == "day303") { rolltype = "1" NSUserDefaults.standardUserDefaults().setObject(usernamestring, forKey: "username") NSUserDefaults.standardUserDefaults().setObject(passwordstring, forKey: "password") NSUserDefaults.standardUserDefaults().setObject(rolltype, forKey: "roll") self.performSegueWithIdentifier("seguetobidderpage", sender: self) } // Retained saved credentials in app delegate.swift and performed navigation after condition check func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let usernamestring = NSUserDefaults.standardUserDefaults().stringForKey("username") let passwordstring = NSUserDefaults.standardUserDefaults().stringForKey("password") let rolltypestring = NSUserDefaults.standardUserDefaults().stringForKey("roll") if (usernamestring == "bidder" && passwordstring == "day303" && rolltypestring == "1") { // Access the storyboard and fetch an instance of the view controller var storyboard = UIStoryboard(name: "Main", bundle: nil) var viewController: BidderPage = storyboard.instantiateViewControllerWithIdentifier("bidderpageID") as! BidderPage // Then push that view controller onto the navigation stack var rootViewController = self.window!.rootViewController as! UINavigationController rootViewController.pushViewController(viewController, animated: true) } // Override point for customization after application launch. return true } Hope it helps !