LoginSignup
133
141

More than 5 years have passed since last update.

Swiftでセグエを利用した画面遷移

Posted at

概要

画面遷移はアプリとして基本的な動作なのに、頭の中でちょっと整理できていないので覚え書き。

ファイル構成

まずは、ファイル構成。UIViewControllerを継承した FirstViewController SecondViewControllerを作成する。

スクリーンショット 2014-11-22 23.50.57.png

Storyboardで、2つのViewControllerを配置し、ラベルとテキストボックスを適当に配置する。

スクリーンショット 2014-11-22 23.51.22.png

そして、それぞれのカスタムクラスを割り当てる。

スクリーンショット 2014-11-22 23.57.42.png

セグエの作成

FirstViewControllerに配置したGoボタンからsegueを伸ばして、SecondViewControllerへセグエ(custome)を作成する。

画面遷移(FirstView → SecondView)

スクリーンショット 2014-11-23 0.06.27.png

作成したセグエにidentifier名を設定する。ここでは、goSecondViewSegueとする。

スクリーンショット 2014-11-23 0.07.29.png

ここまでで、実行してGoボタンを押してみるとFirstViewからSecondViewへ画面遷移するはずである。とても簡単。

画面遷移(SecondView → FirstView)

しかし、このままだと戻れないので、Backボタンを押すとFirstViewへ戻るように設定してやる。
その為に、FirstViewController内に、戻ってきた時にコールされるメソッドを定義してやる。
そうすると、Backボタンを右クリックにてSecondViewにあるExitボタンへ伸ばすと、FirstViewControllerに定義したメソッドが表示されるので設定する。ここでは、backFromSecoundViewというメソッドにした。

FirstViewController.swift
    /*
     * SecondViewから戻ってきた時の処理
     */
    @IBAction func backFromSecondView(segue:UIStoryboardSegue){
        NSLog("FirstViewController#backFromSecondView")
    }

スクリーンショット 2014-11-23 0.29.30.png

FirstViewControllerに定義するのがミソである。結構この方法がややこしく忘れやすい。

そこで実行するとBackボタンから戻ることができるはずである。

ソースコード

ここまでのソースコードは下記。

FirstViewController.swift
import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    /*
     * SecondViewから戻ってきた時の処理
     */
    @IBAction func backFromSecondView(segue:UIStoryboardSegue){
        NSLog("FirstViewController#backFromSecondView")
    }
}
SecondViewController.swift
import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

パラメータの受け渡し

たいていのアプリでは画面遷移の時には、必ずパラメータの受け渡しが必要となってくる。
そこで今回はFirstViewのテキストボックスに入力した値(String)をSecondViewで表示してみる。

まずはSecondViewControllerに、パラメータ受取用のプロパティ変数を宣言しておく。初期化処理の時にデータが表示ラベルとバインドするように代入する。

SecondViewController.swift
import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var paramLabel: UILabel!

    //パラメータ受取用プロパティ
    var param:String = "init param"

    override func viewDidLoad() {
        super.viewDidLoad()
        //パラメータのバインド
        self.paramLabel.text = self.param
    }
}

パラメータを送信するFirstViewController側では、セグエで画面遷移する前に呼ばれるprepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)メソッドをoverrideしてパラメータを設定する。
遷移先であるSecondViewControllerをインスタンス化して画面で入力したテキストボックスの値を設定する。

FirstViewController.swift
    /*
     * 画面遷移
     */
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
        var secondViewController:SecondViewController = segue.destinationViewController as SecondViewController
        secondViewController.param = self.paramText.text
    }

これを実行すると、テキストボックスで入力したデータが、遷移先の画面のラベルに表示される。

ちなみに、入力された値の検証には、shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?)メソッドををoverrideして、問題あればfalseを返すようにするようだ。

FirstViewController.swift
    /*
     * 画面遷移の前に実行する検証メソッド
     */
    override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
        return true
    }

今回はここまで。

Lovely Swift!!!

133
141
4

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
133
141