如何在不使用Storyboard的情况下创build一个新的Swift项目?

在XCode 6中创build一个新项目不允许禁用Storyboard。 您只能selectSwift或Objective-C并使用或不使用核心数据。

我尝试删除故事板,并从项目中删除主故事板,并从didFinishLaunching手动设置窗口

在AppDelegate中我有这样的:

class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow var testNavigationController: UINavigationController func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { testNavigationController = UINavigationController() var testViewController: UIViewController = UIViewController() 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 } } 

但是,XCode给我一个错误:

类“AppDelegate”没有初始化器

任何人都可以成功呢?

您必须将windowtestNavigationControllervariables标记为可选:

 var window : UIWindow? var testNavigationController : UINavigationController? 

Swift类需要在实例化过程中初始化非可选属性:

在创build该类或结构的实例时,类和结构必须将其存储的所有属性设置为适当的初始值。 存储的属性不能处于不确定的状态。

可选types的属性会自动初始化为nil值,表示该属性在初始化期间故意打算具有“没有值”。

当使用可选variables时,请记住用它们解开它们! , 如:

 self.window!.backgroundColor = UIColor.whiteColor(); 

所有它不需要使用Storyboard的rootViewController

1·将AppDelegate.swift更改为:

 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) if let window = window { window.backgroundColor = UIColor.white window.rootViewController = ViewController() window.makeKeyAndVisible() } return true } } 

2·创build一个UIViewControllerViewController子类:

 import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.blue } } 

3·如果您从Xcode模板创build项目:

  1. Info.plist删除键"Main storyboard file base name"的键值对。
  2. 删除故事板文件Main.storyboard

正如你可以在第一个代码片断中看到的,而不是隐式地展开一个可选项,我更喜欢if let解包可选的window属性的语法。 在这里,我使用它就像if let a = a { }以便可选的a成为一个非可选的引用在if语句中具有相同的名称 – a

最后是self. 在它自己的类中引用window属性时是没有必要的。

如果你想用xib初始化你的viewController,并且需要使用导航控制器。 这是一段代码。

 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 } 

试试下面的代码:

 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 } 

我find了与Xcode设置无关的答案,删除了故事板,项目的参考是正确的。 它必须用快速的语法。

代码如下:

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

你可以这样做:

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

我build议你使用控制器和xib

MyViewController.swiftMyViewController.xib

(你可以通过File-> New-> File-> Cocoa Touch Class创build并设置“也创buildXIB文件”,UIViewController的子类为true)

 class MyViewController: UIViewController { ..... } 

并在AppDelegate.swift func application编写下面的代码

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

这应该是工作!

这是一个完整的UINavigationController的快速testing示例

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

你为什么不创build一个空的应用程序? 故事板不是为我创造的…

我们可以在Xcode 6(iOS 8)中创build基于导航的应用程序,而无需使用Storyboard,如下所示:

  • 通过select项目语言作为Swift创build一个空的应用程序。

  • 用接口xib添加新的cocoa触摸类文件。 (如TestViewController)

  • 在swift中,我们只有一个文件与xib ie * .swift文件交互,没有.h和.m文件。

  • 我们可以将xib的控件与iOS 7中的swift文件连接起来。

以下是与控件和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() } } 

AppDelegate.swift文件中的更改

 // // 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) { } } 

http://ashishkakkad.wordpress.com/2014/06/16/create-a-application-in-xcode-6-ios-8-without-storyborard-in-swift-language-上查找代码示例和其他信息-工作与-控制/

更新了Swift 3.0:

 window = UIWindow(frame: UIScreen.main.bounds) window?.backgroundColor = UIColor.white window?.rootViewController = ViewController() window?.makeKeyAndVisible()