Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a sequence of modal/push segues between few view controllers to keep all the push/modal views on the same stack? (using nibs)

I'm trying to create push and modal segues the right way, and from what I understand is to embed my root view controller in a navigation controller in my app delegate...So when I want to create segues they all will be modal/pushed to the same stack..is that right?

I'm really trying to understand this since i'm having some issues doing it correctly.

I want to be able to perform :

FirstViewController > click on a button (modal segue) > SecondViewController > click on a button (push) > ThirdViewController

And from the ThirdViewController I will click on cancel take the ThirdViewController of the stack and get to the SecondViewController click cancel take SecondViewController of the stack and get to the FirstViewController which is my rootviewcontroller and it will be the only view controller in the viewstack...

isn't it how it's suppose to work? please correct me if I'm wrong.

This is how I did it so far: (I know how to dismiss view controllers so I did not add this code)

AppDelegate.h:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "FirstViewController"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (strong, nonatomic) FirstViewController *firstViewController;


@end

AppDelegate.m:

@implementation AppDelegate

@synthesize firstViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    firstViewController = [[FirstViewController alloc]init];


    self.navigationController = [[UINavigationController alloc]initWithRootViewController: firstViewController];

    [self.window setRootViewController:self.navigationController];
    [self.window makeKeyAndVisible];
    return YES;
}

FirstViewController.h:

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface HomeViewController : UIViewController

- (IBAction)goToSecond:(id)sender;

@end

FirstViewController.m:

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)init {
    self = [super initWithNibName:@"FirstViewController" bundle:nil];
    if (self) {
        // Do something
    }
    return self;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)goToSecond:(id)sender {

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController presentViewController: secondViewController animated:YES completion:nil];
}

Now in the second ViewController I'm trying to think how to get to the rood navigation controller so I can add to it another screen instead of creating another navigation controller in SecondViewController.

like image 313
nick shmick Avatar asked Nov 24 '25 20:11

nick shmick


1 Answers

If you first want to use a modal segue and then a push segue, you need to embed the modal view controller into an instance of UINavigationController.

For your setup, it'll look like this:

- (IBAction)goToSecond:(id)sender {
       SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    [self.navigationController presentViewController:navigationController animated:YES completion:nil]; // can be  [self presentViewController:navigationController animated:YES completion:nil] as well...
}

Then you can implement e.g. goToThird in SecondViewController like so:

- (IBAction)goToThird {
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self.navigationController pushViewController:navigationController animated:YES completion:nil];
}
like image 162
nburk Avatar answered Nov 26 '25 11:11

nburk