2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】【FloatingPanel】モーダルを一枚だけ閉じたいのに全部閉じられてしまうのをなんとかする

Last updated at Posted at 2021-03-31

どういうことか

半モーダル(ハーフモーダル?セミモーダル?)をFloatingPanelで実装している。
伝わるのか自信がないが、いくつかの遷移を経由して表示されたViewControllerにて、半モーダルをボタンを押して閉じるという処理を実装するために dismiss を使うと経由してきたViewControllerが全部閉じて振り出しに戻ってしまう。

問題点

前提として

  • 親ViewController(FloatingPanelのDelegateが実装されている)
  • 子ViewController(半モーダルで表示されるページ)

とすると、問題点は子ViewControllerにてFloatingPanelのDelegateメソッドが使えないことにある。
そのためには子ViewControllerから親ViewControllerに処理を委任する必要がある。つまりDelegate(FloatingPanelのDelegateとは別)を使う。

親ViewControllerを ParentViewController
子ViewControllerを ChildrenViewController としよう。

ChildrenViewController.swift
protocol ChildrenViewControllerDelegate {
    func dismissModal()
}

// 中略

    var delegate: ChildrenViewControllerDelegate?

// 中略

    @IBAction func doneButtonAction(_ sender: Any) {
        
        delegate?.dismissModal()
    }
ParentViewController.swift

    // メンバ変数
    let fpc = FloatingPanelController()

// 中略

        // viewDidLoad内
        let storyboard = UIStoryboard(name: "ChildrenViewController", bundle: nil)
        let vc = storyboard.instantiateViewController(identifier: "ChildrenViewController")
        vc.delegate = self

// 中略

extension swift:ParentViewController: ChildrenViewControllerDelegate {
    
    func dismissModal() {
        // この dismiss が子ViewControllerで使いたかった
        fpc.dismiss(animated: true, completion: nil)
    }
}

おわり(´・ω・`)

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?