0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Main.storyboardを使わずXIBファイルを使うためには②

Last updated at Posted at 2025-02-07

前回のあらすじ↓

 
Main.storyboardを使わずXIBファイルを使うためにはどうしたらいいかを勉強しています。
そしてSenceDelegate.swiftも消そうとしています。

 

スクリーンショット 2025-02-07 10.43.18.png

今のファイルは画像のものしかありません。

とりあえずプロジェクトの新規作成をした後に、
「SenceDelegate.swift」
「Main.stroryborad」
を削除しました。

それからやること

①まずはAppDelegateをいじることにしました。

App.swift
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        
        // UIWindowの初期化
        window = UIWindow(frame: UIScreen.main.bounds)

        // ViewControllerをXIBで初期化
        let mainVC = ViewController() // 引数なしでinit呼び出し
        let navController = UINavigationController(rootViewController: mainVC)
        
        window?.rootViewController = navController
        window?.makeKeyAndVisible()
        
        return true
    }
}

②ViewController.swiftでXIBファイルを参照するよう設定

ViewController.swift
import UIKit

class ViewController: UIViewController {

    // XIB ファイルを指定して初期化
    init() {
        super.init(nibName: "ViewController", bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    @IBOutlet weak var Label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white  // 背景色を設定(確認用)
    }
}

③Info.plistの設定を変えてAppDelegate.swiftがMain.storyboradを参照しないようにする
(これが一番難しかった…)

こちらの記事を参考にさせて頂きました。
わかりやすくてありがたかったです。

これで実行するととりあえずアプリが起動したのでほっとしました。

XIB難しい!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?