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

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

提交回复
热议问题