LoginSignup
16
15

More than 5 years have passed since last update.

開いたモーダルダイアログを全部閉じる

Last updated at Posted at 2014-10-04

セットアップなんかで次々とウィザードっぽくダイアログを開く時があると思いますが、最後の画面で、今まで開いたダイアログを全部閉じたい場合があると思います。UI上良い、悪いというのはこの際省きます。

HogeViewController *viewController = [[HogeViewController alloc] initWithNibName:@"HogeViewController" bundle:nil];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:nav animated:YES completion:^{
    }
];

こんな感じで、次々と開きますよね。
今まではdelegateかなんかで覚えておいて、一つ前のViewControllerのクラスを呼び出して

[self dismissViewControllerAnimated:NO completion:NULL];

とかやっていたんですが、下の階層に遡っていってしまってうまく全部閉じれないんですね。
そんなときは、下記のコード一発で全部閉じれちゃいます。

[[UIApplication sharedApplication].delegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

ううむ、もっとはやく知っていればもっとスッキリかけたのに、、
というわけで書き直し中。

16
15
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
16
15