LoginSignup
73
71

More than 5 years have passed since last update.

モーダルビューを出したり消したり

Last updated at Posted at 2013-06-24

UIActionSeetやUIAlertViewみたいに、出してすぐ引っ込めたい。けどそれらよりももっと色々全画面で書きたい、みたいなときに使うのがモーダルビューです。(たぶん)
今回は、メモ用に作ったUIViewControllerであるMemoViewControllerをぱっと出してぱっと消す、という設定で、実装してます。
ちなみにMemoViewControllerはXIBファイルとして作ったので、初期化するときは「init」だけで作れちゃいます。initWithNibName:bundle:みたいにめんどくさくなくて良いですね。

// 遷移元のUIViewController(MemoViewControllerじゃない方)に書きますよ。
// モーダルビューを表示する
-(void)showMemoView{
    MemoViewController *memoViewController = [[MemoViewController alloc] init]; 
    [self presentViewController:memoViewController animated:YES completion:nil];
}

これで、showMemoViewを実行したら、MemoViewControllerの画面がぱっと出てきます。
以前はpresentModalViewController:animated:でしたが、iOS6以降では、presentViewController:animated:completion:を使うそうです。
なのでモーダルビューという言い方はもう古いのかもしれません。

遷移先のMemoViewControllerには、ビューをぱっと消すためのメソッドも実装しましょう。

// モーダルビューを消す
- (void)dismissMemoView {
    [self dismissViewControllerAnimated:YES completion:nil];
}

こちらも、以前はdismissModalViewControllerAnimated:だったのが、
dismissViewControllerAnimated:completion:に変わっています。

73
71
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
73
71