LoginSignup
43
45

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-07-19

頭の中の消しゴムの威力が強すぎる...


MainStoryboardのViewControllerにidentifierをつけよう

まず、値を渡したい側のViewControllerに、以下のようなidentifierをつける。
スクリーンショット 2014-07-19 21.23.42.png

以下のようなコードを書く

FirstViewController.m
- (IBAction)nextButton{
    SecondViewController *secondVC =  [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

    secondVC.secondNum = self.firstNum;
    [self presentViewController:secondVC animated:YES completion:nil];//YESならModal,Noなら何もなし

}
FirstViewController.h
#import "SecondViewController.h"
()
@property int firstNum;

Swift3 or 4

ViewController.swift
func nextButton() {
  let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
  secondVC.secondNum = self.firstNum // FirstVCのpropertyであるfirstNumから、SecondVCのsecondNumに代入する
  present(secondVC, animated: true, completion: nil)
}

俺はpushで画面遷移したいんだ...

ViewController.h
//push
[self.navigationController pushViewController:secondVC animated:YES];

//push 2回閉じるとか
push による画面遷移から「戻る」方法3つ

その他

ViewController.h
//modal 2回閉じる
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
43
45
1

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
43
45