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

More than 3 years have passed since last update.

Swift 画面遷移前に値渡しをする方法2個

Posted at

方法①プロトコル、デリゲートで渡す

//ViewController(画面遷移後)
protocol プロトコル名 {
    func 関数名(送りたいデータ)
}

class ViewController {
.
.
      var delegate:プロトコル名
      
.
.
//前の画面遷移前に戻るタイミングでこれを書く。
self.delegate.関数名(送りたいデータ
}

 //画面遷移前の画面

 class FirstViewController:プロトコル名 {
//delegateメソッドを自身に設定
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            let nextVC = segue.destination as! ViewController
            nextVC.delegate = self
        }
func 関数名送りたいデータ{
  //送りたいデータが入ってくる
}

}

方法②モーダルの場合、

前の画面
 @IBAction func getData(_ unwindSegue: UIStoryboardSegue) {
        //unWindViewController
        guard let vc = unwindSegue.source as? 繊維元VC else { return }
        vc.でプロパティにアクセスしてデータを取ってこれる
    }

遷移もとのVC
 @IBAction func back(_ sender: Any) {
        performSegue(withIdentifier: 設定した識別子, sender: nil)
    }
//StoryBoard上でExitから繊維元のVCに接続して自分で作った関数名がでてくるのでそれを選択。
//その後segueの識別子を選択して完了
1
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
1
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?