【swift】現在のモーダルを閉じる


 

swiftで画面遷移を実装しているときに、現在の画面の「戻る」ボタンを押したときに遷移元画面に戻りたいときがあります。つまり現在開いているビューを閉じる(落とす/もどる)場合、現在の画面のViewControllerで以下のように処理をおこなう。

 

self.dismissViewControllerAnimated(true, completion: nil)

 

 

また、すべての画面を閉じたい時には一番上の階層で以下のようにすればよさそうです。

self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

 

 

 

 
e.x.)

swift_closeModal2

 

kussuue