Creating a navigationController programmatically (Swift)

后端 未结 6 365
盖世英雄少女心
盖世英雄少女心 2020-11-29 19:21

I\'ve been trying to redo the work on my app programmatically. (Without the use of storyboards)

I\'m almost done, except making the navigation controller manually.

6条回答
  •  既然无缘
    2020-11-29 19:32

    Try this one . It will guide you how to use navigation controller.

    Programatically creating UINavigationController in iOS

    AppDelegate.h

        #import 
        #import "LoginViewController.h"
    
        @interface AppDelegate : UIResponder 
    
        @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];
      }
    

    Then when you want to push the other view controller , simple use following code to move to another view controller.

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

提交回复
热议问题