问题描述
我对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.
所以我的问题是:
有没有办法通过在第一个视图中单击我的按钮来设置我创建的新视图作为导航视图的根控制器?
So my questions are: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 get但是我在实现导航项时遇到了问题,因为它们没有显示。
The SecondViewController get's displayed with a navigation bar but I had problems to implements navigation items, because they weren't shown.
我提供的第二个视图控制器是否必须是UIViewController或UINavigationController类型?
Does the second view controller I present have to be of type UIViewController oder 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.
编辑:我现在没有客观的背景和快速学习4周。我感谢任何帮助。
edit: I have no objective-c background and learning swift for about 4 weeks now. I appreciate any help.
推荐答案
您可以添加 UINavigationController
,如下所示:
You can add UINavigationController
like below :
首先,您必须创建 SecondViewController
的对象,
First you have to create object of SecondViewController
,
let objVC: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
或
let objVC: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
或
let objVC: SecondViewController? = SecondViewController()
然后将导航添加到 SecondViewController
let aObjNavi = UINavigationController(rootViewController: objVC!)
如果你想出席然后使用:
If you want to present then use :
self.present(aObjNavi, animated: true) {
}
如果你想推,那么使用:
If you want to push then use :
self.navigationController?.pushViewController(aObjNavi, animated: true)
如果你想设置为根控制器,那么使用:
If you want to set as root controller then use :
let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = aObjNavi
这篇关于如何在代码中以编程方式添加导航控制器,而不是作为初始视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!