LoginSignup
3
1

More than 5 years have passed since last update.

モーダルで表示したUIWebView上のアラートが閉じたときにUIWebViewも道連れにされた話

Posted at

環境

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.

AppleのAPIリファレンスより引用。
UIAlertControllerdismissViewControllerAnimatedが呼ばれた際に、
modalViewControllerdismissViewControllerAnimatedも呼ばれるのが原因ぽい。二重にモーダル表示していれば、UIWebViewの有無関係なしに起こるはず。
UIAlertControllerdismissViewControllerAnimatedではmodalViewControllerの処理が走らないようにするため、リファレンスの言うとおりにmodalViewController自身がモーダルを表示している場合のみ閉じる処理が走るようにする。

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)
}

モーダルの閉じるボタンをこんなコードで実装していたら、効かなくなってしまったので、フラグで制御して改善した。

できあがったコード

WebViewController.swift
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)
    }
}

参考

3
1
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
3
1