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

EhT*_*hTd 106 ios swift xcode6

在XCode 6中创建新项目不允许禁用Storyboard.您只能选择Swift或Objective-C并使用或不使用Core Data.

我尝试删除故事板,从项目中删除主故事板,并从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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,XCode给了我一个错误:

类'AppDelegate'没有初始值设定项

任何人都成功了吗?

tob*_*sdm 87

不使用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
    }
}
Run Code Online (Sandbox Code Playgroud)

2·创建一个ViewController子类UIViewController:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.blue
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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


aka*_*kyy 70

您必须将windowtestNavigationController变量标记为可选:

var window : UIWindow?
var testNavigationController : UINavigationController?
Run Code Online (Sandbox Code Playgroud)

Swift类需要在实例化期间初始化非可选属性:

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

可选类型的属性将自动初始化,值为nil,表示该属性在初始化期间故意具有"无值".

使用可选变量时,请记得用它打开它们!,例如:

self.window!.backgroundColor = UIColor.whiteColor();
Run Code Online (Sandbox Code Playgroud)


War*_*olf 13

如果要使用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
}
Run Code Online (Sandbox Code Playgroud)


PRE*_*MAR 6

请尝试以下代码:

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

}
Run Code Online (Sandbox Code Playgroud)