LoginSignup
88
86

More than 5 years have passed since last update.

StoryBoard上の画面(ViewController)のインスタンスを作る

Posted at

よく忘れてググったり昔のコード見たりするので自分に備忘録として。

UIViewController *controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"hoge"];

@"hoge"の部分はStoryBoardのViewControllerの呼び出したいもののIdenftifierに予め入力しておいた名前を入れる。

StoryBoardを使わない場合、

[[UIViewController alloc]initWithNibName:@"hoge" bundle:nil];

で呼び出すが、StoryBoardの場合、この記述は使えない模様。
コードにより動的な画面生成をする方法を検索すると、initWithNibNameを使うやり方がよくヒットするが、StoryBoardを使っているのであれば上の記述を使えば良い。

おまけ

こっちもよく忘れるので

画面遷移時に次の画面に値を渡すとか

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    MyViewController *controller = [segue destinationViewController];
    controller.nanika = @"hogehoge";
}

※ MyViewControllerとnanikaというプロパティが定義済みの例

88
86
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
88
86