How do I create a new Swift project without using Storyboards?

后端 未结 13 879
不知归路
不知归路 2020-11-29 15:47

Creating a new project in XCode 6 doesn\'t allow to disable Storyboards. You can only select Swift or Objective-C and to use or not Core Data.

I tried deleting the s

相关标签:
13条回答
  • 2020-11-29 16:15

    Try the following code:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.backgroundColor = UIColor.whiteColor()
    
        // Create a nav/vc pair using the custom ViewController class
    
        let nav = UINavigationController()
        let vc = NextViewController ( nibName:"NextViewController", bundle: nil)
    
        // Push the vc onto the nav
        nav.pushViewController(vc, animated: false)
    
        // Set the window’s root view controller
        self.window!.rootViewController = nav
    
        // Present the window
        self.window!.makeKeyAndVisible()
        return true
    
    }
    
    0 讨论(0)
  • 2020-11-29 16:17

    Update: Swift 5 and iOS 13:

    1. Create a Single View Application.
    2. Delete Main.storyboard (right-click and delete).
    3. Delete Storyboard Name from the default scene configuration in the Info.plist file:
    4. Open SceneDelegate.swift and change func scene from:
    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).
        guard let _ = (scene as? UIWindowScene) else { return }
    }
    

    to

     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).x
    
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = ViewController()
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:19

    I recommend you use controller and xib

    MyViewController.swift and MyViewController.xib

    (You can create through File->New->File->Cocoa Touch Class and set "also create XIB file" true, sub class of UIViewController)

    class MyViewController: UIViewController {
       .....    
    }
    

    and In AppDelegate.swift func application write the following code

    ....
    var controller: MyViewController = MyViewController(nibName:"MyViewController",bundle:nil)
    self.window!.rootViewController = controller
    return true
    

    It should be work!

    0 讨论(0)
  • 2020-11-29 16:19

    Here is a complete swift test example for an UINavigationController

            import UIKit
            @UIApplicationMain
            class KSZAppDelegate: UIResponder, UIApplicationDelegate {    
              var window: UIWindow?
              var testNavigationController: UINavigationController?
    
              func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                // Override point for customization after application launch.        
                // Working WITHOUT Storyboard
                // see http://randexdev.com/2014/07/uicollectionview/
                // see http://stackoverflow.com/questions/24046898/how-do-i-create-a-new-swift-project-without-using-storyboards
                window = UIWindow(frame: UIScreen.mainScreen().bounds)
                if let win = window {
                  win.opaque = true    
                //you could create the navigation controller in the applicationDidFinishLaunching: method of your application delegate.    
                  var testViewController: UIViewController = UIViewController()
                  testNavigationController = UINavigationController(rootViewController: testViewController)
                  win.rootViewController = testNavigationController
                  win.backgroundColor = UIColor.whiteColor()
                  win.makeKeyAndVisible()
    // see corresponding Obj-C in https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html#//apple_ref/doc/uid/TP40011313-CH2-SW1
            //      - (void)applicationDidFinishLaunching:(UIApplication *)application {
            //    UIViewController *myViewController = [[MyViewController alloc] init];
            //    navigationController = [[UINavigationController alloc]
            //                                initWithRootViewController:myViewController];
            //    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            //    window.rootViewController = navigationController;
            //    [window makeKeyAndVisible];
                //}
                }
                return true
              }
        }
    
    0 讨论(0)
  • 2020-11-29 16:20

    Why don't you just create an empty application? the storyboard is not created to me...

    0 讨论(0)
  • 2020-11-29 16:21

    I have found the answer it had nothing to do with the xcode setup, removing storyboard and the reference from project is the right thing. It had to do with the swift syntax.

    The code is the following:

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    var testNavigationController: UINavigationController?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    
            self.testNavigationController = UINavigationController()
            var testViewController: UIViewController? = UIViewController()
            testViewController!.view.backgroundColor = UIColor.redColor()
            self.testNavigationController!.pushViewController(testViewController, animated: false)
    
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
            self.window!.rootViewController = testNavigationController
    
            self.window!.backgroundColor = UIColor.whiteColor()
            self.window!.makeKeyAndVisible()
    
            return true
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题