[英]set initial viewcontroller in appdelegate - swift
I would like to set the initial viewcontroller from the appdelegate.我想从 appdelegate 设置初始视图控制器。 I found a really good answer, however it's in Objective C and im having trouble achieving the same thing in swift.
我找到了一个非常好的答案,但是它在目标 C 中,我无法快速实现相同的目标。
Programmatically set the initial view controller using Storyboards 使用 Storyboards 以编程方式设置初始视图控制器
- (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;
}
Anyone able to help?任何人都可以提供帮助?
I want the initial Viewcontroller to be dependent on certain conditions being met using a conditional statement.我希望初始 Viewcontroller 依赖于使用条件语句满足的某些条件。
I used this thread to help me convert the objective C to swift, and its working perfectly.我使用这个线程来帮助我将目标 C 转换为 swift,并且它工作得很好。
Instantiate and Present a viewController in Swift 在 Swift 中实例化并呈现一个 viewController
Swift 2 code:斯威夫特 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 code:斯威夫特 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
}
Try this.尝试这个。 For example: You should use
UINavigationController
as the initial view controller.例如:您应该使用
UINavigationController
作为初始视图控制器。 Then, you can set any view controller as root from the storyboard.然后,您可以将任何视图控制器设置为故事板中的根。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyboard.instantiateInitialViewController() as UINavigationController
let rootViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
return true
}
For new Xcode 11.xxx and Swift 5.xx, where the target it set to iOS 13+.对于新的 Xcode 11.xxx 和 Swift 5.xx,目标设置为 iOS 13+。
For the new project structure, AppDelegate does not have to do anything regarding rootViewController.对于新的项目结构,AppDelegate 不需要对 rootViewController 做任何事情。
A new class is there to handle window(UIWindowScene) class -> 'SceneDelegate' file.有一个新类来处理 window(UIWindowScene) 类 -> 'SceneDelegate' 文件。
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = // Your RootViewController in here
self.window = window
window.makeKeyAndVisible()
}
}
Swift 3, Swift 4:斯威夫特 3、斯威夫特 4:
Change first line to将第一行更改为
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
the rest is same.其余的都是一样的。
Swift 5+:斯威夫特 5+:
Instantiate root view controller from storyboard:从故事板实例化根视图控制器:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: 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 its 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 is called 'Storyboard ID'
let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
return true
}
If you want to use UINavigationController
as root:如果你想使用
UINavigationController
作为 root:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: 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
}
Instantiate root view controller from xib:从 xib 实例化根视图控制器:
It is almost the same, but instead of lines它几乎相同,但不是线条
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController
you'll have to write你必须写
let viewController = YourViewController(nibName: "YourViewController", bundle: nil)
if you are not using storyboard, you can try this如果你不使用故事板,你可以试试这个
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
}
Code for Swift 4.2 and 5 code: Swift 4.2 和 5 代码的代码:
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: "dashboardVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
And for Xcode 11+
and for Swift 5+
:对于
Xcode 11+
and for Swift 5+
:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = // Your RootViewController in here
self.window = window
window.makeKeyAndVisible()
}
}
}
Here is a good way to approach it.这是接近它的好方法。 This example places a navigation controller as the root view controller, and puts the view controller of your choice within it at the bottom of the navigation stack, ready for you to push whatever you need to from it.
这个例子将一个导航控制器作为根视图控制器,并将您选择的视图控制器放在导航堆栈的底部,准备好从它推送您需要的任何内容。
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()
}
To make this example work you would set "MainViewController" as the Storyboard ID on your main view controller, and the storyboard's file name in this case would be "MainStoryboard.storyboard".为了使这个示例工作,您需要将“MainViewController”设置为主视图控制器上的故事板 ID,在这种情况下,故事板的文件名将是“MainStoryboard.storyboard”。 I rename my storyboards this way because Main.storyboard to me is not a proper name, particularly if you ever go to subclass it.
我以这种方式重命名我的故事板,因为 Main.storyboard 对我来说不是一个合适的名字,特别是如果你去继承它。
I had done it in Objective-C.我已经在 Objective-C 中完成了。 I hope it will be useful for you.
我希望它对你有用。
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];
Init ViewController in AppDelegate在 AppDelegate 中初始化 ViewController
[Initial View Controller] [初始视图控制器]
Disable Main.storyboard
禁用
Main.storyboard
General -> Deployment Info -> Main Interface -> remove `Main`
Info.plist -> remove Key/Value for `UISceneStoryboardFile` and `UIMainStoryboardFile`
Add Storyboard ID添加故事板 ID
Main.storyboard -> Select View Controller -> Inspectors -> Identity inspector -> Storyboard ID -> e.g. customVCStoryboardId
[previos] [上一个]
Swift 5 and Xcode 11 Swift 5 和 Xcode 11
Extend UIWindow
扩展
UIWindow
class CustomWindow : UIWindow {
//...
}
Edit generated by Xcode SceneDelegate.swift
由 Xcode
SceneDelegate.swift
生成的编辑
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: CustomWindow!
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "customVCStoryboardId")
//or if your storyboard has `Is Initial View Controller`
let storyboard = UIStoryboard(name: String(describing: SomeViewController.self), bundle: nil)
let initialViewController = storyboard.instantiateInitialViewController()
window = CustomWindow(windowScene: windowScene)
window.rootViewController = initialViewController
window.makeKeyAndVisible()
}
//...
}
[Access to Framework bundle] [访问框架包]
[Get storyboard from a framework] [从框架中获取故事板]
If you're not using the storyboard.如果您不使用故事板。 You can initialize your main view controller programmatically.
您可以以编程方式初始化主视图控制器。
Swift 4斯威夫特 4
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let rootViewController = MainViewController()
let navigationController = UINavigationController(rootViewController: rootViewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
}
And also remove Main
from Deployment Info .并从Deployment Info 中删除
Main
。
I had done in Xcode 8 and swift 3.0 hope it will be useful for u, and its working perfectly.我已经在 Xcode 8 和 swift 3.0 中完成了,希望它对你有用,并且它工作得很好。 Use following code :
使用以下代码:
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
}
And If you are using the navigation controller, then use following code for that:如果您使用的是导航控制器,请使用以下代码:
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
}
Swift 4:斯威夫特 4:
Add these lines inside AppDelegate.swift, within the didFinishLaunchingWithOptions() function...在 AppDelegate.swift 中的 didFinishLaunchingWithOptions() 函数中添加这些行...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Setting the Appropriate initialViewController
// Set the window to the dimensions of the device
self.window = UIWindow(frame: UIScreen.main.bounds)
// Grab a reference to whichever storyboard you have the ViewController within
let storyboard = UIStoryboard(name: "Name of Storyboard", bundle: nil)
// Grab a reference to the ViewController you want to show 1st.
let initialViewController = storyboard.instantiateViewController(withIdentifier: "Name of ViewController")
// Set that ViewController as the rootViewController
self.window?.rootViewController = initialViewController
// Sets our window up in front
self.window?.makeKeyAndVisible()
return true
}
Now, for example, many times we do something like this when we want to either drive the user to a login screen or to a initial setup screenl or back to the mainScreen of the app, etc. If you want to do something like that too, you can use this point as a fork-in-the-road for that.现在,例如,当我们想要将用户引导到登录屏幕或初始设置屏幕或返回应用程序的主屏幕等时,很多时候我们会做这样的事情。如果你也想做类似的事情,你可以使用这个点作为一个岔路口。
Think about it.想想看。 You could have a value stored in NSUserDefaults for instance that held a userLoggedIn Boolean and
if userLoggedIn == false { use this storyboard & initialViewController... } else { use this storyboard & initialViewController... }
您可以将一个值存储在 NSUserDefaults 中,例如持有一个 userLoggedIn 布尔值,
if userLoggedIn == false { use this storyboard & initialViewController... } else { use this storyboard & initialViewController... }
Well all the answers above/below are producing a warning about no entry point in storyboard.好吧,上面/下面的所有答案都产生了关于故事板中没有入口点的警告。
If you want to have 2 (or more) entry view controllers that depend on some condition (say conditionVariable ) then what you should do is:如果您想拥有 2 个(或更多)依赖于某些条件(例如conditionVariable )的入口视图控制器,那么您应该做的是:
Use next code:使用下一个代码:
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 }
Hope this helps.希望这可以帮助。
Here is complete Solution in Swift 4 implement this in didFinishLaunchingWithOptions这是 Swift 4 中的完整解决方案,在 didFinishLaunchingWithOptions 中实现
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isLogin = UserDefaults.standard.bool(forKey: "Islogin")
if isLogin{
self.NextViewController(storybordid: "OtherViewController")
}else{
self.NextViewController(storybordid: "LoginViewController")
}
}
write this Function any where inside Appdelegate.swift在 Appdelegate.swift 中的任何地方写这个函数
func NextViewController(storybordid:String)
{
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let exampleVC = storyBoard.instantiateViewController(withIdentifier:storybordid )
// self.present(exampleVC, animated: true)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = exampleVC
self.window?.makeKeyAndVisible()
}
Just in case you want to do it in the view controller and not in the app delegate: Just fetch the reference to the AppDelegate in your view controller and reset it's window object with the right view controller as it's rootviewController.以防万一您想在视图控制器中而不是在应用程序委托中执行此操作:只需在视图控制器中获取对 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()
For swift 4.0 .对于swift 4.0 。
In your AppDelegate.swift file in didfinishedlaunchingWithOptions method, put the following code.在didfinishedlaunchingWithOptions方法中的AppDelegate.swift文件中,放入以下代码。
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let rootVC = MainViewController() // your custom viewController. You can instantiate using nib too. UIViewController(nib name, bundle)
//let rootVC = UIViewController(nibName: "MainViewController", bundle: nil) //or MainViewController()
let navController = UINavigationController(rootViewController: rootVC) // Integrate navigation controller programmatically if you want
window?.rootViewController = navController
return true
}
Hope it will work just fine.希望它能正常工作。
Swift 5 & Xcode 11斯威夫特 5 和 Xcode 11
So in xCode 11 the window solution is no longer valid inside of appDelegate.因此,在 xCode 11 中,窗口解决方案在 appDelegate 中不再有效。 They moved this to the SceneDelgate.
他们将其移至 SceneDelgate。 You can find this in the SceneDelgate.swift file.
您可以在 SceneDelgate.swift 文件中找到它。
You will notice it now has a var window: UIWindow?
你会注意到它现在有一个
var window: UIWindow?
present.展示。
In my situation I was using a TabBarController from a storyboard and wanted to set it as the rootViewController.在我的情况下,我使用故事板中的 TabBarController 并希望将其设置为 rootViewController。
This is my code:这是我的代码:
sceneDelegate.swift场景委托.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
self.window = self.window ?? UIWindow()//@JA- If this scene's self.window is nil then set a new UIWindow object to it.
//@Grab the storyboard and ensure that the tab bar controller is reinstantiated with the details below.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
for child in tabBarController.viewControllers ?? [] {
if let top = child as? StateControllerProtocol {
print("State Controller Passed To:")
print(child.title!)
top.setState(state: stateController)
}
}
self.window!.rootViewController = tabBarController //Set the rootViewController to our modified version with the StateController instances
self.window!.makeKeyAndVisible()
print("Finished scene setting code")
guard let _ = (scene as? UIWindowScene) else { return }
}
Make sure to add this to the correct scene method as I did here.确保像我在这里所做的那样将其添加到正确的场景方法中。 Note that you will need to set the identifier name for the tabBarController or viewController you are using in the storyboard.
请注意,您需要为故事板中使用的 tabBarController 或 viewController 设置标识符名称。
In my case I was doing this to set a stateController to keep track of shared variables amongst the tab views.在我的例子中,我这样做是为了设置一个 stateController 来跟踪选项卡视图之间的共享变量。 If you wish to do this same thing add the following code...
如果您希望做同样的事情,请添加以下代码...
StateController.swift状态控制器.swift
import Foundation
struct tdfvars{
var rbe:Double = 1.4
var t1half:Double = 1.5
var alphaBetaLate:Double = 3.0
var alphaBetaAcute:Double = 10.0
var totalDose:Double = 6000.00
var dosePerFraction:Double = 200.0
var numOfFractions:Double = 30
var totalTime:Double = 168
var ldrDose:Double = 8500.0
}
//@JA - Protocol that view controllers should have that defines that it should have a function to setState
protocol StateControllerProtocol {
func setState(state: StateController)
}
class StateController {
var tdfvariables:tdfvars = tdfvars()
}
Note: Just use your own variables or whatever you are trying to keep track of instead, I just listed mine as an example in tdfvariables struct.注意:只需使用您自己的变量或您想要跟踪的任何变量,我只是在 tdfvariables 结构中列出了我的变量作为示例。
In each view of the TabController add the following member variable.在 TabController 的每个视图中添加以下成员变量。
class SettingsViewController: UIViewController {
var stateController: StateController?
.... }
Then in those same files add the following:然后在这些相同的文件中添加以下内容:
extension SettingsViewController: StateControllerProtocol {
func setState(state: StateController) {
self.stateController = state
}
}
What this does is allows you to avoid the singleton approach to passing variables between the views.这样做的目的是让您避免在视图之间传递变量的单例方法。 This allows easily for the dependency injection model which is much better long run then the singleton approach.
这很容易让依赖注入模型在长期运行时比单例方法好得多。
iOS 13+ iOS 13+
In the SceneDelegate :在场景委托中:
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options
connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let vc = UIViewController() //Instead of UIViewController() we initilise our initial viewController
window?.rootViewController = vc
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 !
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
}
Open a viewcontroller with SWRevealViewController From App delegate.使用 SWRevealViewController 从 App 委托打开一个视图控制器。
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let swrevealviewcontroller:SWRevealViewController = storyboard.instantiateInitialViewController() as! SWRevealViewController
self.window?.rootViewController = swrevealviewcontroller
self.window?.makeKeyAndVisible()
For Swift 5+ Swift 5+
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let submodules = (
home: HomeRouter.createModule(),
search: SearchRouter.createModule(),
exoplanets: ExoplanetsRouter.createModule()
)
let tabBarController = TabBarModuleBuilder.build(usingSubmodules: submodules)
window.rootViewController = tabBarController
self.window = window
window.makeKeyAndVisible()
}
}
I find this answer helpful and works perfectly for my case when i needed to change the rootviewcontroller if my app user already exist in the keychain or userdefault.如果我的应用程序用户已经存在于钥匙串或 userdefault 中,我发现这个答案很有帮助并且非常适合我的情况,当我需要更改 rootviewcontroller 时。
https://stackoverflow.com/a/58413582/6596443 https://stackoverflow.com/a/58413582/6596443
Swift 5.3 + and iOS 13.0 + Swift 5.3 + 和 iOS 13.0 +
Set your initial ViewController in " SceneDelegate.swift " // Only在“ SceneDelegate.swift ”中设置您的初始 ViewController // 仅
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
setInitialViewController()
window?.makeKeyAndVisible()
}
func setInitialViewController() {
// Set Story board Controller
/*
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
*/
// Set Custom Xib
let vc = FrontVC(nibName: "FrontViewController", bundle: nil)
// Navigation Controller
let nav = UINavigationController(rootViewController: vc)
nav.isNavigationBarHidden = true
window?.rootViewController = nav
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.