2

I have some tasks in the modal controller, I need that when you open another object, only some elements in the controller are replaced. I found the following example In iOS, how to drag down to dismiss a modal?, everything works fine, but I need to ensure that when I close my controller Hiding and not triggering an event deinit. I would like to have two events: dismiss and hide.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Feanon
  • 121
  • 1
  • 8
  • A modal view controller is exactly that: Modal. That means that while it is being presented, the user should not be able to interact with anything underneath it. It is not clear from your question why you want to hide it, but perhaps the best course of action is to save its state, dismiss it, and present it again when you need it back. – Nicolas Miari Jul 13 '17 at 07:16
  • 1
    @Nicolas Miari thanks for answer, i need access to modal view without preloading data, modal window like Deezer app or Apple Music. – Feanon Jul 13 '17 at 07:30
  • Sorry, I am unfamiliar with those apps so I still don't understand what you are trying to achieve. Please edit your question and explain the UI flow step by step, without making assumptions on what we know (we don't know anything about your app or its data model). – Nicolas Miari Jul 13 '17 at 07:32
  • @Nicolas Miari I do not know how else to explain. I want the same window as in any ios player application, for example: spotify, tidal, apple music, deezer there are a lot of them. They have a modal window with a player, but when it closes it turns into a minibar, I need it, but without a minibar. – Feanon Jul 13 '17 at 07:43
  • 1
    @SashaVasilev You can reuse a ViewController, instead of creating a new one each time. Also, if you feel more comfortable with pushing a ViewController instead of a model, you can do that and customize the segue to behave like a model. – thedp Jul 13 '17 at 08:09
  • 1
    I think you just have to save the memory reference of the viewController, and the re-present it again. It will not deallocate even if you dismiss it as long as there is a memory reference – Zonily Jame Jul 13 '17 at 10:26

1 Answers1

1

What you should do is to keep a memory reference of the viewController.

For example:

class ParentViewController: UIViewController {
    // place this here to keep it in ParentViewController's memory
    var subViewController: SubViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        // initialize the subViewController and set it as the attribute
        self.subViewController = SubViewController()
    }

    func showSub() {
        if let unwrappedSubViewController = self.subViewController {
            self.present(unwrappedSubViewController, animated: true, completion: nil)
        }
    }

    func dismissSub() {
        self.subViewController?.dismiss(animated: true, completion: nil)
    }

}

class SubViewController: UIViewController {
    .. some properties here
}

As long as your ParentViewController lives your subViewController's memory reference lives, therefore it will not be deinited/deallocated

If you want to remove the reference completely then just do this.

// use optional binding for safety
self.subViewController?.dismiss(animated: true) { [weak self]
    self?.subViewController = nil
}
Zonily Jame
  • 5,053
  • 3
  • 30
  • 56