0

I have three main view controllers A, B, C, which are presented in the order by show segue. All the three view controllers are embedded in their own navigation controller. After dismissing a modal controller D presented on C, I would like to go back to controller A and reload data before it appears.

I referred to the solution from this question to unwind segue and wrote an action in controller A (I use AViewController to represent the class of controller A):

@IBAction func prepareForUnwindSegue(segue: UIStoryboardSegue?) {
    if let destController = segue?.destinationViewController as? AViewController {
        destController.reload()
    }
}

override func canPerformUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject) -> Bool {
    return self.respondsToSelector(action)
}

I dragged-exit between controller A and C by the above action, and name the unwind segue unwindToA. Then in the class of controller C, named CViewcontroller, I call the modal view D in one ActionSheet as follow:

let presentDAction = UIAlertAction(title: "Present D", style: .Default, handler: {(_) -> Void in
    let controller: AnyObject? = self.storyboard!.instantiateViewControllerWithIdentifier("DView")
    if let c = controller as? DViewController {
        let navi = UINavigationController(rootViewController: c)
        navi.modalPresentationStyle = UIModalPresentationStyle.FullScreen

        // the following block wait and execute after view controller D dismissed
        c.refreshAndUnwind = {[weak self](refresh) in 
            if let weakSelf = self {
                if(refresh) {
                    weakSelf.performSegueWithIdentifier("unwindToA", sender: weakSelf)
                }
            }
        }
    }
 })

The view controller did successfully unwind along with reloading data. However, it seems that the navigation bar remains still on controller C, and its title disappear. I have tried to drag-exit the navigation controller between A and C instead and modify the unwind segue action call to weakSelf.navidationController!.performSegueWithIdentifier("unwindToA", sender: weakSelf), but it doesn't work. Did I do anything wrong on my segue unwinding that cause this problem?

Community
  • 1
  • 1
whitney13625
  • 533
  • 1
  • 7
  • 27

2 Answers2

1

Sometimes using storyboard is not the best way to do things. I'd do this like this in your case:

    var viewControllers = self.navigationController!.viewControllers
    var controllerA = viewControllers[0] as! ControllerA
    controllerA.reloadStuff()
    var newViewControllers = [ controllerA ]
    self.navigationController!.setViewControllers(newViewControllers, animated: true)

The code is much cleaner this way. You can set the navigationItem's title in viewDidAppear on each view if you have problems with it, you can be sure that it will be always called.

FruitAddict
  • 2,002
  • 13
  • 16
0

So the problem is simple: After I tried to modify my storyboard and remove those not-necessary navigation controllers, it performs just fine.

whitney13625
  • 533
  • 1
  • 7
  • 27