LoginSignup
204
205

More than 3 years have passed since last update.

Swift(UIKit)の画面遷移

Last updated at Posted at 2015-03-20

画面遷移とは

test.gif

基本的な画面遷移として、

階層的なアプリを作りたい・NavigationBarをいい感じに付けて表示したいって時はよく使うのが show

スクリーンショット 2015-03-15 23.38.44.png

下から上にノレンを開けるような画面遷移として present modally
を使う

1. MainStoryboardでoption + Drag&Drop でくっつける

始めに覚えるやり方ですね

2. UIStroyboardSegueでの画面遷移

  • Segueのidentifierで分岐とか、unwindSegueなど

swiftで画面遷移:UINavigationController

3. 遷移先のViewControllerのインスタンスを作成しての画面遷移

画面間で値を渡せる

スクリーンショット 2015-03-21 8.36.18.png

    @IBAction func onClickMyButton(sender: UIButton){
        // 遷移するViewを定義する.このas!はswift1.2では as?だったかと。
        let secondViewController: SecondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("secondVC") as! SecondViewController
        // アニメーションを設定する.
        //secondViewController.modalTransitionStyle = UIModalTransitionStyle.PartialCurl
        // 値渡ししたい時 hoge -> piyo
        //secondViewController.piyo = self.hoge
        // Viewの移動する.
        self.presentViewController(secondViewController, animated: true, completion: nil)
    }

実は、この画面遷移のアニメーションは非表示に出来ちゃう(Stroyboardでやると確か非表示に出来なかったと思う)

animatedをfalseにするだけ
self.presentViewController(secondViewController, animated: false, completion: nil)

【まとめ】コードで画面遷移!

present modally 戻る

self.dismissViewControllerAnimated(true, completion: nil)

present modally 2個前の画面に戻る

self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

show をコードで

self.navigationController?.pushViewController(secondViewController, animated: true)

show 戻る

self.navigationController?.popViewControllerAnimated(true)

show 初めの画面に戻る

self.navigationController?.popToRootViewControllerAnimated(true)

【注意!】showはnavigationControllerがnilになってないコトが必要

下記のような操作をして
-> NavigationViewController ->(rootViewController)-> FirstViewController のようにしてあげましょう

スクリーンショット 2015-03-21 23.39.48.png

SwiftUI

SwiftUI時代の画面遷移


参考記事

Objective-Cです

iOSメモ - コードで画面遷移と値渡し


次回)UILabelやUIViewをコードで生成するか、MainStoryboardでIBOutletで生成するなど

204
205
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
204
205