user2490003
user2490003

Reputation: 11900

How to transition between any two ViewControllers in Cocoa Touch?

I'm fairly new iOS development. I have multiple viewControllers for my app, for example -

Each of these screens has its own viewController.

What's the best way to transition between any two arbitrary viewControllers with an animation?

My current approach is -

  1. Keep a reference to each viewController in AppDelegate.m
  2. Keep changing the window's root controller as needed

This is both cumbersome and seems pretty inefficient, plus I'm not quite sure how to incorprate animated transitions here.

I see some examples with UINavigationController, but it seems like that operates as a "stack" of views that you can go into and then back out of. I'm not looking to keep a history here, just switch from any view to another.

Any good ways to accomplish this?

Thanks!

Upvotes: 0

Views: 133

Answers (3)

Yariv Nissim
Yariv Nissim

Reputation: 13343

As a beginner I believe the easiest solution for you it to use a Storyboard and create a segue between your View Controller.

Inside your ViewController you should override prepareForSegue: method to pass data to the segue.destinationViewController

Upvotes: 0

Hardik Shekhat
Hardik Shekhat

Reputation: 1878

Try this Code.

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) UINavigationController *navigationController;
@property (strong,nonatomic) LoginViewController *loginVC;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "LoginViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

   self.loginVC = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
   self.loginVC.title = @"Login Page";

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

   self.window.rootViewController = self.navigationController;
   [self.window makeKeyAndVisible];
}

LoginViewController.h

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

@interface LoginViewController : UIViewController

@property (strong,nonatomic)MyProfileViewController *myProfileVC;

@end

Login button action in LoginViewController.m file

- (IBAction)pushMyProfileView:(id)sender
{
    self.myProfileVC = [[MyProfileViewController alloc]initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:self.myProfileVC animated:YES];
}

Upvotes: 2

Andrea
Andrea

Reputation: 26385

Basically you can achieve that by using:

  1. Apple containers (UINavigationController, UITabbarController, UISplitController)
  2. Containment API
  3. Using "normal" view controller and modal presentation

Containment API is probably what you are looking for, you create a UIViewController that is responsible to manage the presentation of its child view controllers. Here is Apple Documentation, there are plenty of examples inside.

Upvotes: 0

Related Questions