Help us understand the problem. What is going on with this article?


More than 3 years have passed since last update.


Swift2.3, Xcode8.2, iOS 10。


  • baseViewController上でUIWebViewを保持するmodalViewControllerをモーダル表示
  • UIWebViewで開いたwebページ上のfileタグを押下
  • UIAlertControllerがモーダルで表示される
  • いずれかの項目を選択し、UIAlertControllerが閉じる際にmodalViewControllerも閉じ、baseViewControllerが再び表示される。


The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

If you want to retain a reference to the view controller'€™s presented view controller, get the value in the presentedViewController property before calling this method.


override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
    if nil != self.presentedViewController {
        super.dismissViewControllerAnimated(flag, completion: completion)


// 閉じる
@objc private func close() {
    self.dismissViewControllerAnimated(true, completion: nil)



final class WebViewController: UIViewController {

    // 閉じるボタンが押されたフラグ
    private var closeBtnFlag: Bool = false

    @IBOutlet private weak var webView: UIWebView!


    // 自身の上に重なって開いたモーダルなviewControllerが閉じた際、自身まで一緒に閉じるのを避ける
    override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
        if self.closeBtnFlag || nil != self.presentedViewController {
            super.dismissViewControllerAnimated(flag, completion: completion)
            self.closeBtnFlag = false

    // 閉じる
    @objc private func close() {
        // フラグを立てて閉じる
        self.closeBtnFlag = true
        self.dismissViewControllerAnimated(true, completion: nil)


Swift, Ruby on Rails とかやってます。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away