環境
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リファレンスより引用。
UIAlertController
のdismissViewControllerAnimated
が呼ばれた際に、
modalViewController
のdismissViewControllerAnimated
も呼ばれるのが原因ぽい。二重にモーダル表示していれば、UIWebView
の有無関係なしに起こるはず。
UIAlertController
のdismissViewControllerAnimated
では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)
}
モーダルの閉じるボタンをこんなコードで実装していたら、効かなくなってしまったので、フラグで制御して改善した。
できあがったコード
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)
}
}