How can you reload a ViewController after dismissi

2019-03-14 03:50发布

I have a first tableViewController which opens up a second tableViewcontroller upon clicking a cell. The second view controller is presented modally (Show Detail segue) and is dismissed with:

self.dismissViewControllerAnimated(true, completion: {})

At this point, the second view controller slides away and reveals the first view controller underneath it. I would then like to reload the first view controller. I understand that this may require use of delegate functions, but not sure exactly how to implement it

2条回答
我只想做你的唯一
2楼-- · 2019-03-14 04:28

You can simply reaload your data in viewDidAppear:, but that might cause the table to be refreshed unnecessarily in some cases.

A more flexible solution is to use protocols as you have correctly guessed.

Let's say the class name of your first tableViewController is Table1VC and the second one is Table2VC. You should define a protocol called Table2Delegate that will contain a single method such as table2WillDismissed.

protocol Table2Delegate {
    func table2WillDismissed()
}

Then you should make your Table1VC instance conform to this protocol and reload your table within your implementation of the delegate method.

Of course in order for this to work, you should add a property to Table2VC that will hold the delegate:

weak var del: Table2Delegate?

and set its value to your Table1VC instance.

After you have set your delegate, just add a call to the delegate method right before calling the dismissViewControllerAnimated in your Table2VC instance.

del?.table2WillDismissed()
self.dismissViewControllerAnimated(true, completion: {})

This will give you precise control over when the table will get reloaded.

查看更多
Anthone
3楼-- · 2019-03-14 04:50

I solved it a bit differently since I don't want that dependancy.

And this approach is intended when you present a controller modally, since the presenting controller wont reload when you dismiss the presented.

Anyway solution!

Instead you make a Singleton (mediator)

protocol ModalTransitionListener {
    func popoverDismissed()
}

class ModalTransitionMediator {
    /* Singleton */
    class var instance: ModalTransitionMediator {
        struct Static {
            static let instance: ModalTransitionMediator = ModalTransitionMediator()
        }
        return Static.instance
    }

private var listener: ModalTransitionListener?

private init() {

}

func setListener(listener: ModalTransitionListener) {
    self.listener = listener
}

func sendPopoverDismissed(modelChanged: Bool) {
    listener?.popoverDismissed()
}
}

Have you Presenting controller implement the protocol like this:

class PresentingController: ModalTransitionListener {
//other code
func viewDidLoad() {
    ModalTransitionMediator.instance.setListener(self)
}
//required delegate func
func popoverDismissed() {
    self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
    yourTableViev.reloadData() (if you use tableview)
}
}

and finally in your PresentedViewController in your viewDid/WillDisappear func or custom func add:

ModalTransitionMediator.instance.sendPopoverDismissed(true)
查看更多
登录 后发表回答