如何在代码中以编程方式添加导航控制器而不是作为初始视图控制器 [英] How to add a navigation controller programmatically in code but not as initial view controller

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

问题描述

我对 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 如下:

首先你要创建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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆