Programmatically creating UINavigationController in iOS

前端 未结 7 2177
孤街浪徒
孤街浪徒 2020-12-09 22:54

I am new to iOS. And I want to use navigation controller in my application but I have no any idea how to do it. So can any one guide me step by step for creating navigation

相关标签:
7条回答
  • 2020-12-09 23:09

    In appDelegate.h

    @property (strong, nonatomic) UINavigationController *navController;
    

    and set the delegate UINavigationControllerDelegate and synthesise object in appDelegate.m now,

    appDelegate.m

    you can set navigation controller in didFinishLaunchingWithOptions method

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        frstVwCntlr = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
        self.navController = [[UINavigationController alloc] initWithRootViewController:self.frstVwCntlr];
        self.window.rootViewController = self.navController;
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    In the above code , your firstViewController is set to UINavigationController and UINavigationController added to UIWindow like

    self.window.rootViewController = self.navController

    Hope this may help you

    0 讨论(0)
  • 2020-12-09 23:13

    You can creat UINavigationController in Appdelegate and set your first viewcontroller on it.

    0 讨论(0)
  • 2020-12-09 23:14

    If you want to create everything programmatically you have to do it in AppDelegate.

    But if you don't want to do it programmatically, then just select the ViewController in Storyboard then select menu options:

    Editor > Embed In > Navigation Controller

    0 讨论(0)
  • 2020-12-09 23:20

    Here is the code that you should write in app delegate.

    UIViewController *vc=[[UIViewController alloc]initWithNibName:@"vc1" bundle:nil];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    view.backgroundColor=[UIColor redColor];
    [vc setView:view];
    
    self.navme=[[UINavigationController alloc]initWithRootViewController:vc];
    self.window.rootViewController = self.navme;
    
    0 讨论(0)
  • 2020-12-09 23:22
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ImageViewController2"];
    [self.navigationController pushViewController:dealVC animated:YES];
    

    where ImageViewController2 is a class name

    0 讨论(0)
  • 2020-12-09 23:26

    For Swift 3.0, using filter:

    let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
    self.navigationController!.popToViewController(desiredController, animated: true)
    
    0 讨论(0)
提交回复
热议问题