【Xcode】EntryPointを変更する方法
もくじ
- 環境
- Entry Pointとは
- StoryBordから変更する
- コードから変更する
- [おまけ]初回起動時のみ特定の画面を表示させる
0. 環境
- MacBook Air M1
- Xcode 12.4
1. Entry Pointとは
- プログラムの開始される位置
- この記事では開始される画面の位置も含まれる
画面上で見ると以下の画像の矢印部分がEntry Pointを表している
アシスタントエディタで確認すると以下赤枠部分
2. StoryBordから変更する
ドラックアンドドロップで矢印を移動させる
Utilites areaから移動させる
Attributes inspector内
のIs Initial View Controller
にチェクを入れる
3. コードから変更する
1. Main.storybordの遷移させたいViewControllerにStoryboad ID
を設定する
Identity Inspector
内のSotryboard ID
を設定
- 今回はこのように設定
- 1画面目 : FirstView
- 2画面目 : SecondView
2. SceneDelegate.swift
何も変更していないデフォルトの状態だとこのようなコードになっていると思います
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}scene
メソッド内に以下のコードのコードを追記します- この時、
guard let _ =
の_
の部分をwindow
に変更しています。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let storybard = UIStoryboard(name: "Main",bundle: nil)
// 初めて起動しているか
if(lanchIsFIrstTme()){
window?.rootViewController = storybard.instantiateViewController(identifier: "FirstView")
firstLanch()
}else{
window?.rootViewController = storybard.instantiateViewController(identifier: "SecondView")
}
window?.makeKeyAndVisible()
}- この時、
4. [おまけ]初回起動時のみ特定の画面を表示させる
一つ前で実装した切り替え処理とユーザーデフォルト機能を組み合わせて、初回起動かどうかを判定する
確認用メソッドと登録用メソッドを作成
1
2
3
4
5
6
7
8
9private let STORED_KEY = "lanched"
func firstLanch(){
return UserDefaults.standard.set(true,forKey: STORED_KEY)
}
func lanchIsFIrstTme() -> Bool{
return !UserDefaults.standard.bool(forKey: STORED_KEY)
}一つ前で実装したコードを改変
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let storybard = UIStoryboard(name: "Main",bundle: nil)
// 初めて起動しているか
if(lanchIsFIrstTme()){
window?.rootViewController = storybard.instantiateViewController(identifier: "FirstView")
firstLanch()
}else{
window?.rootViewController = storybard.instantiateViewController(identifier: "SecondView")
}
window?.makeKeyAndVisible()
}
こんな感じになればOK
参考
【Xcode】EntryPointを変更する方法