【问题标题】:Creating a navigationController programmatically (Swift)以编程方式创建一个 navigationController (Swift)
【发布时间】:2015-05-01 19:59:56
【问题描述】:

我一直在尝试以编程方式重做我的应用程序上的工作。 (不使用情节提要)

我差不多完成了,除了手动制作导航控制器。

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

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

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

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

编辑:

我忘了提到它是在 Swift 中的。

【问题讨论】:

  • 请注意,在绝大多数情况下,您可以在情节提要上执行此操作...stackoverflow.com/a/22981726/294884 然后您可以关闭古怪的按钮栏,这与执行此操作“一样好”以编程方式。
  • @ThomasKilian 这是怎么重复的?另一个是 Objective C 但这是 Swift??
  • @user3390652 查看编辑历史。当 OP 询问时,它没有用 Swift 标记。

标签: ios swift uinavigationcontroller


【解决方案1】:

Swift 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+:和 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()
}

【讨论】:

  • 没关系,我意识到我哪里出错了。将其改回 mainView 并执行“var mainView = NameOfYourViewController()”并且它起作用了。非常感谢您的帮助!
  • 如果我想将它添加到已经有 uitabbarcontroller 的视图中怎么办?
  • @TomSawyer ,在 tabbar 添加控制器之前,使用您的控制器创建 UINavigationController 对象并使用该导航对象添加到 UITabbarcontroller
  • 您也可以使用UINavigationController(rootViewController: _) 初始化程序,而不是将具有一个元素的数组分配给viewControllers 属性。但这只是为了节省一行代码:)
  • 这对我来说似乎是错误的(虽然我知道它有效,但我已经使用过)。 self.window.rootViewController 是 UIViewController 类型,它应该在 Swift 对象中被分配一个 UINavigationController。
【解决方案2】:

我建议使用这个框架来启动你的 AppDelegate:

1) 尽可能使用 let

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
}

【讨论】:

    【解决方案3】:

    “AppDelegate”类型的值没有成员“窗口”

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

    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
    }
    

    【讨论】:

    • 谢谢,我想在单视图控制器中设置 naviBar,并且无法弄清楚它的约束。现在我知道这是错误的。应在 AppDelegate 或 SceneDelegate 中设置为全局使用。
    【解决方案4】:

    这里是 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()
        }
    }
    

    【讨论】:

    • 如何使它与 tabbarcontrolle 一起工作,以便显示标题?
    • 我不完全确定我知道您的要求。您只需像这样将 UITabBarController 添加为 rootViewController -> window.rootViewController = MyTabBarController()
    【解决方案5】:

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

    Programatically creating UINavigationController in iOS

    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

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

    然后当你想要推送另一个视图控制器时,只需使用以下代码移动到另一个视图控制器。

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

    【讨论】:

    • 嘿,非常感谢您的回复!我知道这完全是我自己的错,我只是将其添加为标签而忘记提及。但我正在迅速对其进行编码。不幸的是,我不知道如何将该代码翻译成 swift。
    • 答案应该是很快的
    • @UmairAfzal 好的,如果您需要,我会尽快发布 swift 的更新代码。
    • @HardikShekhat 实际上我确实需要请参考我的问题。 stackoverflow.com/questions/37606778/…
    • @UmairAfzal 我会在那里快速发布答案。
    【解决方案6】:
     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() 
    }
    

    【讨论】:

    • 这个答案不是通用的,并且依赖于一个不属于该语言的类。该答案也没有显示该类的导入,也没有说明该类的来源或获取方式。完全有可能在没有任何 CommonMethods 的情况下重写它,从而减少代码行和更通用的答案。此处还有其他样式代码,NavigationController 正常工作不需要这些代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2017-03-06
    相关资源
    最近更新 更多