在appdelegate中设置初始viewcontroller-迅速


147

我想从appdelegate设置初始viewcontroller。我找到了一个很好的答案,但是在目标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依赖于使用条件语句满足的某些条件。


Answers:


279

我使用此线程来帮助我将目标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
}

1
在swift 2应用中找不到故事板“主要”,也找不到“ Storyboard.storyboard”
Async-

5
这是在appdelegate中设置初始视图控制器的当前标准方法吗?我问,因为它看起来像是一小撮骇客(对不起,@ Abs)。
rigdonmr

10
@rigdonmr如果应用程序将情节提要设置为“主界面”,则将自动加载窗口,并将其根视图控制器设置为情节提要的初始视图控制器。如果视图控制器需要根据条件进行更改,或者应用程序没有主界面,则可以初始化a UIWindow并以编程方式设置其根视图控制器。
2016年

2
令人惊讶(但很好)的是,以这种方式手动实例化视图控制器似乎阻止了iOS实例化默认视图控制器。我可能期望两者都被加载。此行为记录在某处吗?
帕特·尼迈耶

2
@MayankJain可以在Swift 3上正常工作,只需要翻译早期swift的一些代码
JAB 2016年

42

试试这个。例如:您应该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
}

看到 我的故事板屏幕。


您是否为故事板中的视图控制器设置了故事板ID?参见joxi.ru/l2ZYxZqS8JOomJ
protikhonoff 2014年

是的,已经设置好了,我认为它已经成功地打开了视图控制器,但是没有“显示”它。有任何想法吗?
Abubakar Moallim 2014年

没错

我想我找到了问题。您应该在情节提要中选中“是Initial View Controller”。joxi.ru/8AnNbQlhq3QOAO
protikhonoff 2014年

但我想使用条件语句更改初始viewcontroller。
Abubakar Moallim 2014年

24

对于Swift 3,Swift 4:

从情节提要实例化根视图控制器:

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
    }

如果要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)

22

如果您不使用情节提要,可以尝试一下

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
}

1
笔尖名称指的是什么?
Abubakar Moallim 2014年

@Abs nibName是要加载以实例化视图的笔尖的名称。
rashii 2014年

方向过渡动画将停止使用此功能。
user3427013

我已经对这个大声赞了……“一定要初始化窗口,然后设置框架,必须初始化窗口,然后设置框架,初始化窗口,然后设置框架”
克里斯·艾伦森 Chris Allinson)

18

对于新的Xcode 11.xxx和Swift 5.xx,目标设置为iOS 13+。

对于新的项目结构,AppDelegate不必对rootViewController做任何事情。

那里有一个新的类来处理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()
    }

}

1
非常感激。
阿齐姆·塔卢达

14

这是解决它的好方法。本示例将导航控制器作为根视图控制器,并将您选择的视图控制器放入导航堆栈的底部,以供您根据需要推送任何内容。

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”设置为情节提要ID,在这种情况下,情节提要的文件名将为“ MainStoryboard.storyboard”。我之所以对故事板进行重命名,是因为Main.storyboard对我来说不是一个恰当的名称,尤其是如果您要对其进行子类化的话。


11

我已经在Objective-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];

您如何在情节提要中设计View Controller。请提供帮助。
Poonam'9

拖动视图控制器并设置标识的情节提要ID:<ViewControllerName>并访问您的视图控制器...
Patel Jigar 2015年

@PatelJigar如何在loginvc中设置
Uma Madhavi

像这样的UITabBarController调用视图控制器* tbc = [self.storyboard InstantiateViewControllerWithIdentifier:@“ ContentViewController”]; [自身presentViewController:tbc动画:是完成:无];
Patel Jigar 2015年

7

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()
}

对于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()
         }
    }
}

self.window提供错误类型'AppDelegate'的值没有成员'window',有什么想法吗?
Joseph Astrahan

@JosephAstrahan在调用变量之前先声明一个变量。喜欢- var window: UIWindow?
贾米尔·哈斯宁·塔米姆

@JosephAstrahan再次检查我的答案。我添加了变量。
贾米尔·哈斯宁·塔米姆

谢谢,这可以帮助很多!
Joseph Astrahan

And for Xcode 11+ and for Swift 5+部分对我有很大帮助。谢谢男人
多伦多

6

我曾经在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
}

嗨... Mayank,我已经在Xcode 8和swift 3.0中完成了使用此解决方案后出现的错误,因为它起作用了
Kunal 2016年

6

斯威夫特4:

将这些行添加到didFinishLaunchingWithOptions()函数内的AppDelegate.swift中...

    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
}

现在,例如,当我们想将用户带到登录屏幕或初始设置屏幕或返回到应用程序的主屏幕等时,很多时候我们会做类似的事情。如果您也想做类似的事情,您可以将此点用作实现此目的的分叉点。

想一想。例如,您可以在NSUserDefaults中存储一个值,该值包含一个userLoggedIn布尔值和if userLoggedIn == false { use this storyboard & initialViewController... } else { use this storyboard & initialViewController... }


它在添加了1个ViewController的新项目中不起作用。始终从初始视图控制器启动。Xcode 11.2哪里可能有问题?
Oleh H

5

好吧,上面/下面的所有答案都会产生有关故事板中没有切入点的警告。

如果要有2个(或更多)依赖于某些条件(例如conditionVariable)的入口视图控制器,那么您应该做的是:

  • 在您的Main.storyboard中创建UINavigationController 而无需 rootViewController的,将其设置为入口点
  • 在视图控制器中创建2个(或更多个)“显示”按钮,为其分配一些ID,例如ID1ID2
  • 使用下一个代码:

    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
       }

希望这可以帮助。


1
“情节提要中没有入口点”错误是由于可以删除的项目配置所致。转到项目>信息>自定义iOS目标属性,然后删除“主故事板文件库名称”属性。该警告不应再出现。
orangemako

5

如果您不使用情节提要。您可以以编程方式初始化主视图控制器。

斯威夫特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
    }
}

Main从“ 部署信息”中删除。

在此处输入图片说明


这与我在Swift 4.2,XCode 11.3,IOS
9〜13.3.1中一起使用

4

这是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")

    }
}

在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()
}

3

以防万一,您想在视图控制器中而不是在应用程序委托中执行此操作:只需在视图控制器中获取对AppDelegate的引用,并使用正确的视图控制器作为rootviewController重置其window对象。

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()

3

对于快速4.0

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
}

希望它会很好。


3
工作..如果您没有在滑板中设置初始viewController,那么您必须添加window = UIWindow(frame:UIScreen.main.bounds)
Punit

3

Swift 5和Xcode 11

因此,在xCode 11中,窗口解决方案在appDelegate内部不再有效。他们将其移至SceneDelgate。您可以在SceneDelgate.swift文件中找到它。

您会发现它现在有var window: UIWindow?礼物。

在我的情况下,我使用情节提要中的TabBarController并将其设置为rootViewController。

这是我的代码:

sceneDelegate.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 }
    }

确保像我在这里一样将其添加到正确的场景方法中。请注意,您将需要为故事板中使用的tabBarController或viewController 设置标识符名称

如何设置情节提要ID

就我而言,我这样做是为了设置stateController来跟踪选项卡视图之间的共享变量。如果您希望这样做,请添加以下代码...

StateController.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()
}

注意:只需使用您自己的变量或您尝试跟踪的变量,我只是在tdfvariables结构中列出了我的示例。

在TabController的每个视图中,添加以下成员变量。

    class SettingsViewController: UIViewController {
    var stateController: StateController?
.... }

然后在这些相同的文件中添加以下内容:

extension SettingsViewController: StateControllerProtocol {
  func setState(state: StateController) {
    self.stateController = state
  }
}

这样可以避免在视图之间传递变量的单例方法。这很容易实现依赖注入模型,从长远来看,它比单例方法好得多。


3

禁用 Main.storyboard

General -> Deployment Info -> Main Interface -> remove `Main` 
Info.plist -> remove Key/Value for `UISceneStoryboardFile` and  `UIMainStoryboardFile`

添加情节提要ID

Main.storyboard -> Select View Controller -> Inspectors -> Identity inspector -> Storyboard ID -> e.g. customVCStoryboardId

Swift 5和Xcode 11

延伸 UIWindow

class CustomWindow : UIWindow {
    //...
}

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")

        window = CustomWindow(windowScene: windowScene)
        window.rootViewController = initialViewController
        window.makeKeyAndVisible()
    }

    //...
}

可以将根控制器设置为初始视图控制器window.rootViewController = UIStoryboard(name:“ Main”,bundle:nil).instantiateInitialViewController()
Kamran Khan

1
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 !

1
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
}

1

从App委托使用SWRevealViewController打开一个视图控制器。

 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()

0

对于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()
        }
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.