Segue between ViewControllers within different NavControllers

Hi all,


Is it possible to segue between viewcontrollers that are contained inside different navigation controllers? Is it advisable? When I set up a segue from my button to the viewcontroller I want to segue to, one that is contained within a different navigation controller, my button stops working and no segue happens.

Answered by Claude31 in 380137022

The point is that if you define an IBAction, it is triggered and then the segue event is not passed.


What you could do is add a performSegue in the IBAction.


Don't forget to close the thread if solved.

>....viewcontrollers that are contained inside different navigation controllers(?) Is it advisable?


Are you going to the already instantiated viewController in that other nav controller or are you creating a new instantiation of the same viewController?


If the former - Usually not. Jumping from deep within one nav controller to deep within another may be a confusing architecture for the user. (I entered the bedroom closet but when I walked out I was in the livingroom!) You might want to rethink your structure from the user's perspective.


If the later, you can instantiate a new viewController from the same class under the current nav controller. Give it a little different title or something so the user is clear where they are in your architecture. (I entered the bedroom closet; it looks like the livingroom closet but when I walked out I was back in the bedroom.)

You may want to confirm your process via the View Controller Programming Guide for iOS / Using Segues


"Use segues to define the flow of your app’s interface. A segue defines a transition between two view controllers in your app’s storyboard file. The starting point of a segue is the button, table row, or gesture recognizer that initiates the segue. The end point of a segue is the view controller you want to display. A segue always presents a new view controller, but you can also use an unwind segue to dismiss a view controller."


Just be sure to respect the 'Order of Containment' when it comes to how you're structuring your nav controllers.


Curious if you're using UIStoryboardSegue?


Otherwise, show some code as applies and any errors, print statement results in the console, etc.

Thanks for your reply. It's a shame that pictures can't be posted here. I have a tabbar controller with two screens - a standard viewcontroller homescreen, and a tableview controller. The homescreen processes data and saves it to coredata, the tableviewcontroller displays it. The tableview controller is inside a navcontroller, and the tableview controller cell segues to a details screen. I am simply trying to call the details screen from a button in the homescreen (i'm using the details screen for double purpose, as a save screen), which is inside it's own navController. The button works without any segue setup, but when I setup a segue to the details screen that I want to call, the button stops working. It won't even run a print command to the console.

This is 'the later' case so my 'objection' of the user getting lost in the structure is not valid. You are not using the previous instantiation of the details screen class, you are creating a new instantiation of that class and trying to display that new instantiation.


Sorry, I don't do seques - I do things programmatically because it is more straightforward. (I think your experience is again confirming this for me.)

No probs. Thanks for your interest. Unless I can solve my problem I'm thinking that programmatically might be the route I take too. In my situation would you recommend setting up a xib and calling that? When I programmatically call the details screen it comes without the navigation bar (i guess because it is outside of the NavigationController) which has the buttons that I need. A separate dedicated viewcontroller might be the better option.

Here is code to instantiate a new view controller from the class "VariousFunctions" and present it in a nav controller:

        VariousFunctions *variousFunctions=[[VariousFunctions alloc] init];
        // do something in the instantiation specific for this function \\
        //    [variousFunctions someMethod:@"perform this function"];  
        UINavigationController *navigationController = 
             [[UINavigationController alloc] initWithRootViewController:variousFunctions];
        navigationController.modalPresentationStyle=UIModalPresentationFormSheet;
        [self presentViewController:navigationController animated:YES completion:^{
        }];

place -(void)someMethod; in the .h file and place code for someMethod in the .m file


If you want to use an xib file then use initWithNibName: (rather than init: above)

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621359-initwithnibname

Thanks. I've added it to my snippets library.

Segue between ViewControllers within different NavControllers
 
 
Q