0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Swift]アプリの立ち上げ、画面遷移をコードで行う方法

Posted at

複雑な画面遷移を行うアプリでStoryboardで行うと、Segueが入り混じりとんでもないことになるのが予想されます。

それを避けるため、コードで遷移する方法を簡単に実装します。

完成形

スクリーンショット-2021-09-25-22.37.56.png

目的

可読性が良い、改修のしやすいコードにする

事前準備

①Info.plistのMain storyboard file base name を削除
②SceneDelegateを削除(iOS12以前にも対応可能にするため)

コード

①AppDelegate

AppDelegate.swift
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let window = UIWindow(frame: UIScreen.main.bounds) 
        #//ウィンドウをインスタンス化する

        self.window = window 
        Router.showRoot(window: window) 
        #// 初期Viewの取得はRouterクラスで行う

        return true
    }
}

②Router

画面遷移の処理はこのクラスで全て行う。

Router.swift

import UIKit

final class Router {

   # // アプリ起動時にrootViewを取得する処理
    static func showRoot(window: UIWindow?) {
        let firstStoryboard = UIStoryboard(name: "First", bundle: nil)
        let firstVC = firstStoryboard.instantiateInitialViewController() as! 
        FirstViewController
        let nav = UINavigationController(rootViewController: firstVC) 
     #// ナビゲーションコントローラーを定義。引数で最下層となるViewを指定
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
    }

   # // 2つ目のViewへ画面遷移する処理
    static func showSecond(fromVC: UIViewController) {
        let secondStoryboard = UIStoryboard(name: "Second", bundle: nil)
        let secondVC = secondStoryboard.instantiateInitialViewController() as! SecondViewController
        show(fromVC: fromVC, nextVC: secondVC)
    }

   # // 実際に画面を遷移させる処理
    private static func show(fromVC: UIViewController, nextVC: UIViewController) {
        if let nav = fromVC.navigationController {
            nav.pushViewController(nextVC, animated: true)
        } else {
            fromVC.present(nextVC, animated: true, completion: nil)
        }
    }  
}


③FirstViewController

立ち上げた時に出る画面

FirstViewController.swift

import UIKit

class FirstViewController: UIViewController {
   # // ボタンを押した時の処理
    @IBAction func tapGoSecond(_ sender: UIButton) {
        Router.showSecond(fromVC: self)
    }
}

④SecondViewController

遷移した後の画面

SecondViewController

import UIKit

class SecondViewController: UIViewController {
    // ボタンを押した時の処理
    @IBAction func tapGoThird(_ sender: UIButton) {
        Router.showThird(fromVC: self)
    }   
}

追記

以上がコードで書く画面遷移のやり方となります。

複雑な画面遷移を必要とするアプリの場合、このように切り離すことで、可読性が良くなり改修もしやすくなると思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?