在iOS中以编程方式创建UINavigationController

use*_*619 6 uinavigationcontroller ios

我是iOS新手.我想在我的应用程序中使用导航控制器,但我不知道如何做到这一点.因此,任何人都可以一步一步地指导我在我的应用程序中创建导航.

Bha*_*anu 14

appDelegate.h中

@property (strong, nonatomic) UINavigationController *navController;
Run Code Online (Sandbox Code Playgroud)

现在设置委托UINavigationControllerDelegate和合成对象appDelegate.m,

appDelegate.m

你可以在didFinishLaunchingWithOptions方法中设置导航控制器

- (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;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,您的firstViewController被设置为UINavigationControllerUINavigationController添加到UIWindowlike

self.window.rootViewController = self.navController

希望这可以帮到你


use*_*619 1

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ImageViewController2"];
[self.navigationController pushViewController:dealVC animated:YES];
Run Code Online (Sandbox Code Playgroud)

其中 ImageViewController2 是类名