ios 以编程方式创建导航控制器 (Swift)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28793331/
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-31 05:03:22  来源:igfitidea点击:

Creating a navigationController programmatically (Swift)

iosswiftuinavigationcontroller

提问by MLyck

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.

我几乎完成了,除了手动制作导航控制器。

I've been doing some research but I can't find any documentation of implementing this manually. (I started making the app as a Single View Application)

我一直在做一些研究,但我找不到任何手动实现它的文档。(我开始将应用程序作为单一视图应用程序)

Currently, I only have 1 viewcontroller. And of course the appDelegate

目前,我只有 1 个视图控制器。当然还有 appDelegate

The navigation Controller, will be used throughout all pages in the application.

导航控制器将在应用程序的所有页面中使用。

If anyone could help me out, or send a link to some proper documentation for doing this programmatically it would be greatly appreciated.

如果有人可以帮助我,或发送一些适当文档的链接以编程方式执行此操作,将不胜感激。

EDIT:

编辑:

I forgot to mention it's in Swift.

我忘了提到它在 Swift 中。

回答by Jogendra.Com

Swift 1, 2:

斯威夫特 1、2:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
   var nav1 = UINavigationController()
   var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
   nav1.viewControllers = [mainView]
   self.window!.rootViewController = nav1
   self.window?.makeKeyAndVisible()
}

Swift 4+: and Swift 5+

Swift 4+:和 Swift 5+

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.main.bounds)
   let nav1 = UINavigationController()
   let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
   nav1.viewControllers = [mainView]
   self.window!.rootViewController = nav1
   self.window?.makeKeyAndVisible()
}

回答by telenaut

I would recommend starting your AppDelegate with this skeleton:

我建议用这个骨架启动你的 AppDelegate:

1) use letwherever you can!

1)尽可能使用let

2) UINavigationController has the rootViewControllerproperty.

2) UINavigationController 具有rootViewController属性。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
    let navigationController = UINavigationController(rootViewController: viewController)

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    return true
}

回答by candyline

Value of type 'AppDelegate' has no member 'window'

“AppDelegate”类型的值没有成员“window”

For those building newer projects with SceneDelegate.swift, you can use the 'var window: UIWindow?' in SceneDelegate instead of the removed 'var window' in AppDelegate

对于那些使用 SceneDelegate.swift 构建新项目的人,您可以使用“var window: UIWindow?” 在 SceneDelegate 中而不是在 AppDelegate 中删除的“var window”

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window?.windowScene = windowScene
    window?.makeKeyAndVisible()

    let viewController = ViewController()
    let navViewController = UINavigationController(rootViewController: viewController)
    window?.rootViewController = navViewController
}

回答by Hardik Shekhat

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

试试这个。它将指导您如何使用导航控制器。

Programatically creating UINavigationController in iOS

在 iOS 中以编程方式创建 UINavigationController

AppDelegate.h

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

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];
}

回答by Linus

Here is another take in the SceneDelegate class:

这是 SceneDelegate 类的另一个例子:

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        if let windowScene = scene as? UIWindowScene {

                let window = UIWindow(windowScene: windowScene)    
                let navController = UINavigationController()
                let viewController = ViewController()

                navController.viewControllers = [viewController]            
                window.rootViewController = navController
                self.window = window
                window.makeKeyAndVisible()
        }
}

回答by Nikunj Patel

 self.window = UIWindow(frame: UIScreen.main.bounds) 
 let storyboard = UIStoryboard(name: "Main", bundle: nil) 
 let storyboard_Secondary = UIStoryboard(name: "Secondary", bundle: nil) 
 var initialViewController = UIViewController() 

 let aUser = CommonMethods.loadCustomObject("\(Constants.kUserProfile)") as? User  
 if aUser?.respCode == 1 { 
    initialViewController = storyboard_Secondary.instantiateViewController(withIdentifier: "MainTabVC")
    UIApplication.shared.statusBarStyle = .lightContent
    let navigationController = UINavigationController(rootViewController: initialViewController)
    navigationController.isNavigationBarHidden = true
    self.window!.rootViewController = navigationController
    self.window!.makeKeyAndVisible() 
}