<i id='SZhDk'><tr id='SZhDk'><dt id='SZhDk'><q id='SZhDk'><span id='SZhDk'><b id='SZhDk'><form id='SZhDk'><ins id='SZhDk'></ins><ul id='SZhDk'></ul><sub id='SZhDk'></sub></form><legend id='SZhDk'></legend><bdo id='SZhDk'><pre id='SZhDk'><center id='SZhDk'></center></pre></bdo></b><th id='SZhDk'></th></span></q></dt></tr></i><div id='SZhDk'><tfoot id='SZhDk'></tfoot><dl id='SZhDk'><fieldset id='SZhDk'></fieldset></dl></div>

    <tfoot id='SZhDk'></tfoot>
  • <legend id='SZhDk'><style id='SZhDk'><dir id='SZhDk'><q id='SZhDk'></q></dir></style></legend>

    <small id='SZhDk'></small><noframes id='SZhDk'>

          <bdo id='SZhDk'></bdo><ul id='SZhDk'></ul>

        如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器

        How to add a navigation controller programmatically in code but not as initial view controller(如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器)
          <bdo id='DVqyI'></bdo><ul id='DVqyI'></ul>
          <i id='DVqyI'><tr id='DVqyI'><dt id='DVqyI'><q id='DVqyI'><span id='DVqyI'><b id='DVqyI'><form id='DVqyI'><ins id='DVqyI'></ins><ul id='DVqyI'></ul><sub id='DVqyI'></sub></form><legend id='DVqyI'></legend><bdo id='DVqyI'><pre id='DVqyI'><center id='DVqyI'></center></pre></bdo></b><th id='DVqyI'></th></span></q></dt></tr></i><div id='DVqyI'><tfoot id='DVqyI'></tfoot><dl id='DVqyI'><fieldset id='DVqyI'></fieldset></dl></div>
            <tbody id='DVqyI'></tbody>
          <tfoot id='DVqyI'></tfoot>
          • <legend id='DVqyI'><style id='DVqyI'><dir id='DVqyI'><q id='DVqyI'></q></dir></style></legend>

                <small id='DVqyI'></small><noframes id='DVqyI'>

                • 本文介绍了如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 swift 和 iOS 还比较陌生,找不到适合我的问题的答案或任何帮助.

                  I am relatively new to swift and iOS and can't find an answer or any help for my problem that works.

                  当我单击上一个视图中的按钮时,我想创建一个导航控制器视图.我以前的视图是在最后一页上带有按钮的入职培训.我成功地将按钮连接到我的下一个视图,但我无法使视图像导航控制器一样.例如,当显示导航栏时,我无法向其添加导航项.

                  I want to create a navigation controller view when I click on a button in my previous view. My previous view is an on-boarding with a button on the last page. I successfully connected the button to my next view but I am not able to make the view act like a navigation controller. For example, when a navigation bar is displayed, I am not able to add navigation items to it.

                  有没有办法通过单击第一个视图中的按钮将我创建的新视图设置为导航视图的根控制器?

                  Is there a way to set the new view I create by clicking my button in the first view to be the root controller for my navigation view?

                  我的代码的简短版本:

                  class ViewController: UIViewController {
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
                          button.backgroundColor = .red
                          button.tintColor = .white
                          button.setTitle("Test", for: .normal)
                          button.addTarget(self, action: #selector(change), for: .touchUpInside)
                          view.addSubview(button)
                      }
                  
                      func change () {
                          let otherView = SecondViewController()
                          self.present(otherView, animated: true, completion: nil)
                      }
                  
                  }
                  
                  class SecondViewController: UINavigationController {
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          view.backgroundColor = .yellow
                      }
                  
                  }
                  

                  SecondViewController 使用导航栏显示,但我在实现导航项时遇到了问题,因为它们没有显示出来.

                  The SecondViewController gets displayed with a navigation bar but I had problems implementing navigation items, because they weren't shown.

                  我展示的第二个视图控制器必须是 UIViewController 或 UINavigationController 类型吗?

                  Does the second view controller I present have to be of type UIViewController or UINavigationController?

                  我知道使用情节提要有更简单的方法来实现我的目标,但我认为我通过代码自己引用而不是通过将它们拖出情节提要来创建它们来更好地理解它.

                  I am aware that there are easier ways to achieve my goal with the use of the storyboard but in my opinion I understand it better by making my own references via code instead of creating them by dragging them out of the storyboard.

                  我没有 Objective-C 背景,现在学习 Swift 大约 4 周了.

                  I have no Objective-C background and learning Swift for about 4 weeks now.

                  推荐答案

                  你可以像下面这样添加 UINavigationController :

                  You can add UINavigationController like below :

                  首先你必须创建SecondViewController的对象,

                  First you have to create object of SecondViewController,

                  let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
                  

                  或者

                  let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
                  

                  或者

                  let myViewController: SecondViewController? = SecondViewController()
                  

                  然后将 Navigation 添加到 SecondViewController

                  Then add Navigation to SecondViewController

                  let myNavigationController = UINavigationController(rootViewController: myViewController!)
                      
                  

                  如果您想展示,请使用:

                  If you want to present then use :

                  self.present(myNavigationController, animated: true) { 
                  }
                  

                  如果要推送,请使用:

                  self.navigationController?.pushViewController(myNavigationController, animated: true)
                  

                  如果您想设置为根控制器,请使用:

                  If you want to set as root controller then use :

                  let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
                  appDelegate.window?.rootViewController = myNavigationController
                  

                  这篇关于如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ViewController in UINavigationController orientation change(UINavigationController 中的 ViewController 方向更改)
                  How to get the previous viewcontroller that pushed my current view(如何获取推送我当前视图的上一个视图控制器)
                  The correct way to set a light status bar text color in iOS 7 based on different ViewControllers(iOS 7中基于不同ViewControllers设置灯光状态栏文字颜色的正确方法)
                  View being blocked by UITransitionView after being presented(呈现后被 UITransitionView 阻止的视图)
                  Show UITabBar when UIViewController pushed(推送 UIViewController 时显示 UITabBar)
                  presentViewController not supporting orientation in iOS 6(presentViewController 在 iOS 6 中不支持方向)
                      <tbody id='YttDk'></tbody>

                    <small id='YttDk'></small><noframes id='YttDk'>

                    <tfoot id='YttDk'></tfoot>
                      <i id='YttDk'><tr id='YttDk'><dt id='YttDk'><q id='YttDk'><span id='YttDk'><b id='YttDk'><form id='YttDk'><ins id='YttDk'></ins><ul id='YttDk'></ul><sub id='YttDk'></sub></form><legend id='YttDk'></legend><bdo id='YttDk'><pre id='YttDk'><center id='YttDk'></center></pre></bdo></b><th id='YttDk'></th></span></q></dt></tr></i><div id='YttDk'><tfoot id='YttDk'></tfoot><dl id='YttDk'><fieldset id='YttDk'></fieldset></dl></div>

                      • <legend id='YttDk'><style id='YttDk'><dir id='YttDk'><q id='YttDk'></q></dir></style></legend>

                            <bdo id='YttDk'></bdo><ul id='YttDk'></ul>