在 iOS 中以编程方式创建 UINavigationController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22981610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:56:16  来源:igfitidea点击:

Programmatically creating UINavigationController in iOS

iosuinavigationcontroller

提问by user3418619

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 in my application.

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

采纳答案by user3418619

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

其中 ImageViewController2 是一个类名

回答by Bhanu

In appDelegate.h

appDelegate.h

@property (strong, nonatomic) UINavigationController *navController;

and set the delegate UINavigationControllerDelegateand synthesise object in appDelegate.mnow,

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

appDelegate.m

appDelegate.m

you can set navigation controller in didFinishLaunchingWithOptionsmethod

您可以在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;
}

In the above code , your firstViewController is set to UINavigationControllerand UINavigationControlleradded to UIWindowlike

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

self.window.rootViewController = self.navController

self.window.rootViewController = self.navController

Hope this may help you

希望这可以帮助你

回答by sleepwalkerfx

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

如果您想以编程方式创建所有内容,则必须在 AppDelegate 中进行。

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

但是,如果您不想以编程方式执行此操作,则只需在 Storyboard 中选择 ViewController,然后选择菜单选项:

Editor > Embed In > Navigation Controller

Editor > Embed In > Navigation Controller

回答by iDeveloper

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

您可以在 Appdelegate 中创建 UINavigationController 并在其上设置您的第一个视图控制器。

回答by Satheeshwaran

So for creating a UINavigationController programatically without using storyboards, go to your app delegate and do the following. Create two properties, window and viewController

因此,要在不使用故事板的情况下以编程方式创建 UINavigationController,请转到您的应用程序委托并执行以下操作。创建两个属性,window 和 viewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor=[UIColor clearColor];

    self.viewController = [[YourFirstViewController alloc] initWithNibName:@"YourFirstViewController" bundle:nil];
    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];

    // Override point for customization after application launch.
    return YES;
}

回答by jigar Thakkar

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;

回答by Ved Rauniyar

For Swift 3.0, using filter:

对于 Swift 3.0,使用过滤器:

let desiredController = self.navigationController!.viewControllers.filter { ##代码## is YourController }.first!
self.navigationController!.popToViewController(desiredController, animated: true)