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

后端 未结 13 878
不知归路
不知归路 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:05

    You must mark the window and testNavigationController variables as optional:

    var window : UIWindow?
    var testNavigationController : UINavigationController?
    

    Swift classes require non-optional properties to be initialized during the instantiation:

    Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

    Properties of optional type are automatically initialized with a value of nil, indicating that the property is deliberately intended to have “no value yet” during initialization.

    When using optional variables, remember to unwrap them with !, such as:

    self.window!.backgroundColor = UIColor.whiteColor();
    
    0 讨论(0)
  • 2020-11-29 16:07

    Updated for Swift 3.0:

    window = UIWindow()
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()
    
    0 讨论(0)
  • 2020-11-29 16:09

    If you want to Initialize your viewController with xib and and need to use navigation controller. Here is a piece of code.

    var window: UIWindow?
    var navController:UINavigationController?
    var viewController:ViewController?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
        viewController = ViewController(nibName: "ViewController", bundle: nil);
        navController = UINavigationController(rootViewController: viewController!);
    
        window?.rootViewController = navController;
        window?.makeKeyAndVisible()
    
        return true
    }
    
    0 讨论(0)
  • 2020-11-29 16:09

    You can just do it like this:

    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        var IndexNavigationController: UINavigationController?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
            var IndexViewContoller : IndexViewController? = IndexViewController()
            self.IndexNavigationController = UINavigationController(rootViewController:IndexViewContoller)
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window!.rootViewController = self.IndexNavigationController
            self.window!.backgroundColor = UIColor.whiteColor()
            self.window!.makeKeyAndVisible()
            return true
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:11

    We can create navigation-based application without storyboard in Xcode 6 (iOS 8) like as follows:

    • Create an empty application by selecting the project language as Swift.

    • Add new cocoa touch class files with the interface xib. (eg. TestViewController)

    • In the swift we have only one file interact with the xib i.e. *.swift file, there is no .h and .m files.

    • We can connect the controls of xib with swift file same as in iOS 7.

    Following are some snippets for work with the controls and Swift

    //
    //  TestViewController.swift
    //
    
    import UIKit
    
    class TestViewController: UIViewController {
    
        @IBOutlet var testBtn : UIButton
    
        init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
            // Custom initialization
        }
    
        @IBAction func testActionOnBtn(sender : UIButton) {
            let cancelButtonTitle = NSLocalizedString("OK", comment: "")
    
            let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
    
            // Create the action.
            let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel) { action in
                NSLog("The simple alert's cancel action occured.")
            }
    
            // Add the action.
            alertController.addAction(cancelAction)
    
            presentViewController(alertController, animated: true, completion: nil)
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    }
    

    Changes in AppDelegate.swift file

    //
    //  AppDelegate.swift
    //
    
    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        var navigationController: UINavigationController?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window!.backgroundColor = UIColor.whiteColor()
            self.window!.makeKeyAndVisible()
    
            var testController: TestViewController? = TestViewController(nibName: "TestViewController", bundle: nil)
            self.navigationController = UINavigationController(rootViewController: testController)
            self.window!.rootViewController = self.navigationController
    
            return true
        }
    
        func applicationWillResignActive(application: UIApplication) {
    }
    
        func applicationDidEnterBackground(application: UIApplication) {
        }
    
        func applicationWillEnterForeground(application: UIApplication) {
        }
    
        func applicationDidBecomeActive(application: UIApplication) {
        }
    
        func applicationWillTerminate(application: UIApplication) {
        }
    
    }
    

    Find code sample and other information on http://ashishkakkad.wordpress.com/2014/06/16/create-a-application-in-xcode-6-ios-8-without-storyborard-in-swift-language-and-work-with-controls/

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

    In iOS 13 and above when you create new project without storyboard use below steps:

    1. Create project using Xcode 11 or above

    2. Delete storyboard nib and class

    3. Add new new file with xib

    4. Need to set root view as UINavigationController SceneDelegate

    5. add below code 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 }

      if let windowScene = scene as? UIWindowScene { self.window = UIWindow(windowScene: windowScene) let mainController = HomeViewController() as HomeViewController let navigationController = UINavigationController(rootViewController: mainController) self.window!.rootViewController = navigationController self.window!.makeKeyAndVisible() } }

    0 讨论(0)
提交回复
热议问题