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

How to add a navigation controller programmatically in code but not as initial view controller(如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器)
本文介绍了如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)