web-dev-qa-db-ja.com

ストーリーボードを使用せずに新しいSwiftプロジェクトを作成するにはどうすればよいですか?

XCode 6で新しいプロジェクトを作成しても、ストーリーボードを無効にすることはできません。 Swiftまたは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'には初期化子がありません

誰もこれに成功していますか?

105
EhTd

windowおよびtestNavigationController変数をオプションとしてマークする必要があります。

var window : UIWindow?
var testNavigationController : UINavigationController?

Swiftクラスでは、インスタンス化中にオプションではないプロパティを初期化する必要があります。

クラスおよび構造は、そのクラスまたは構造のインスタンスが作成されるまでに、格納されているすべてのプロパティを適切な初期値に設定する必要があります。格納されたプロパティを不確定な状態のままにすることはできません。

オプションのタイプのプロパティは、nilの値で自動的に初期化されます。これは、プロパティが初期化中に「値がまだない」ことを意図的に意図していることを示します。

オプションの変数を使用する場合は、次のような!でそれらをアンラップすることを忘れないでください。

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

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・ViewControllerUIViewControllerサブクラスを作成します。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.blue
    }
}

3・Xcodeテンプレートからプロジェクトを作成した場合:

  1. "Main storyboard file base name"からキーInfo.plistのキーと値のペアを削除します。
  2. ストーリーボードファイルMain.storyboardを削除します。

最初のコードスニペットでわかるように、オプションの暗黙的なアンラップの代わりに、オプションのwindowプロパティをアンラップするためのif let構文が好きです。ここでは、if let a = a { }のように使用しているため、オプションのaは、ifという同じ名前のa-ステートメント内のオプションではない参照になります。

最後に、独自のクラス内でwindowプロパティを参照する場合、self.は不要です。

87
tobiasdm

XibでviewControllerを初期化し、Navigation Controllerを使用する必要がある場合。これがコードです。

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

次のコードを試してください:

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

}
6
PREMKUMAR

次のようにできます:

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

私はそれがXcodeのセットアップとは何の関係もないという答えを見つけました。ストーリーボードとプロジェクトからの参照を削除するのは正しいことです。 Swift構文に関係していました。

コードは次のとおりです。

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
    }

}
2
EhTd

Swift 3.0用に更新:

window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
2
Tiago Martinho

コントローラーとxibを使用することをお勧めします

MyViewController.SwiftおよびMyViewController.xib

(File-> New-> File-> Cocoa Touch Classを使用して作成し、UIViewControllerのサブクラス「XIBファイルも作成」をtrueに設定できます)

class MyViewController: UIViewController {
   .....    
}

およびAppDelegate.Swiftfunc applicationで次のコードを記述します

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

うまくいくはずです!

2
Yi Feng Xie

UINavigationControllerの完全なSwiftテスト例です

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

空のアプリケーションを作成してみませんか?ストーリーボードは私には作成されていません...

0
allemattio

Xcode 6(iOS 8)では、次のようにストーリーボードなしでナビゲーションベースのアプリケーションを作成できます。

  • プロジェクト言語としてSwiftを選択して、空のアプリケーションを作成します。

  • インターフェイスxibを使用して、新しいココアタッチクラスファイルを追加します。 (例:TestViewController)

  • Swiftには、xibと対話するファイルが1つしかありません。つまり、*。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でコードサンプルとその他の情報を検索します-コントロールと言語と作業/

0
Ashish Kakkad