Sinan Samet
Sinan Samet

Reputation: 6752

Segue through navigationcontroller

enter image description here

So I have a button in [2] and it pushes to [3] through the Navigation Controller so I can go back to [2] with the "Back" button in the toolbar. This all works fine.

In [4] I have a button too and I want it to go to [3]. But it should also go through the navigation controller so that when I press "Back" I can return to [2] again.

So actually I want the button on [4] to go like [ 1 ][ 2 ][ 3 ] so that I can return to [2] from [3]

@IBAction func showKaart(sender: AnyObject) {
 performSegueWithIdentifier("menuToKaart", sender: sender)
}

Upvotes: 2

Views: 200

Answers (2)

ViTUu
ViTUu

Reputation: 1204

If I understand your question, I believe try it.

Mind your root view controller is vc[2], ok? You push from vc[2] > vc[3], next press back and return to vc[2], that's ok! You push from vc[4] > vc[3], next press back and return to vc[4], but you need back to vc[2]?

For this logic you can create your custom behavior to vc[3]

For your vc[3] I do this to control behavior.

class ViewController3:UIViewController{

    var backToRoot:Bool = false;

    override func viewDidLoad() {
        super.viewDidLoad();
        self.hideAndAddNewBackButton();
    }

    private func hideAndAddNewBackButton(){
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
        self.navigationItem.leftBarButtonItem = newBackButton;
    }

    func back(sender: UIBarButtonItem) {
        if backToRoot{
            self.navigationController?.popToRootViewControllerAnimated(true);
        }else{
            self.navigationController?.popViewControllerAnimated(true)
        }
    }

    func needBackToRoot(){
        backToRoot = true;
    }
}

Now in vc[4] I think you can modify the back button behavior of your vc[3] I mind two way to do with your vc[4]

First using performSegue.

class ViewController4PerfomSegue:UIViewController{

    @IBAction func pressButtonToPushViewController3(sender:AnyObject?){
        self.performSegueWithIdentifier("showViewController3", sender: nil);
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let viewController3 = segue.destinationViewController as? ViewController3{
            viewController3.needBackToRoot();
        }
    }

}

Second with push manual in your navigation view controller

class ViewController4Push:UIViewController{

    @IBAction func pressButtonToPushViewController3(sender:AnyObject?){
        let viewController3 = ViewController3(nibName: "ViewController3", bundle: nil);
        viewController3.needBackToRoot();
        self.navigationController?.pushViewController(viewController3, animated: true);
    }
}

Edit: new way to instantiate from storyboard

class ViewController4Push:UIViewController{

    @IBAction func pressButtonToPushViewController3(sender:AnyObject?){
        if let viewController3 = storyboard!.instantiateViewControllerWithIdentifier("ViewController3") as? ViewController3{
            viewController3.needBackToRoot();
            self.navigationController?.pushViewController(viewController3, animated: true);
        }
    }
}

Edit: removing when new view was presented

class ViewController4Push:UIViewController, UINavigationControllerDelegate{

    @IBAction func pressButtonToPushViewController3(sender:AnyObject?){
        if let viewController3 = storyboard!.instantiateViewControllerWithIdentifier("ViewController3") as? ViewController3{
            viewController3.needBackToRoot();
            self.navigationController?.delegate = self;
            self.navigationController?.pushViewController(viewController3, animated: true);
        }
    }

    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        self.navigationController?.delegate = nil;
        self.dismissViewControllerAnimated(false, completion: nil);
        //or self.removeFromParentViewController();
    }
}

I hope help you, if I understand the problem

Edit: after talk in chat with Sinan we resolve it with simple way, just force back present the root element of application

class ViewCustomController:UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad();
        self.hideAndAddNewBackButton();
    }

    private func hideAndAddNewBackButton(){
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
        self.navigationItem.leftBarButtonItem = newBackButton;
    }

    func back(sender: UIBarButtonItem) {
        if let viewControllerRoot = storyboard!.instantiateViewControllerWithIdentifier("ViewControllerRoot") as? ViewControllerRoot{
            self.navigationController?.pushViewController(viewController2, animated: true);
        }
    }
}

Upvotes: 1

Kelvin Lau
Kelvin Lau

Reputation: 6781

[4] needs to be embedded in a navigation controller. After that, change you @IBAction to use pushViewController:Animated: on the navigationController instead of performSegueWithIdentifier

Upvotes: 0

Related Questions