ios 如何在代码中以编程方式添加导航控制器而不是作为初始视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42809834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a navigation controller programmatically in code but not as initial view controller
提问by iOS-Trainee
I am relatively new to swift and iOS and can't find an answer or any help for my problem that works.
我对 swift 和 iOS 比较陌生,无法为我的问题找到答案或任何帮助。
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?
有没有办法通过单击第一个视图中的按钮将我创建的新视图设置为导航视图的根控制器?
A short version of my code:
我的代码的简短版本:
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
}
}
The SecondViewController gets displayed with a navigation bar but I had problems implementing navigation items, because they weren't shown.
SecondViewController 与导航栏一起显示,但我在实现导航项时遇到了问题,因为它们没有显示。
Does the second view controller I present have to be of type UIViewController or UINavigationController?
我呈现的第二个视图控制器必须是 UIViewController 或 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.
我知道使用故事板有更简单的方法来实现我的目标,但在我看来,我通过代码创建我自己的引用而不是通过将它们拖出故事板来创建它们更好地理解它。
Edit: I have no Objective-C background and learning Swift for about 4 weeks now.
编辑:我没有 Objective-C 背景并且学习 Swift 大约 4 周了。
回答by VRAwesome
You can add UINavigationController
like below :
您可以添加UINavigationController
如下:
First you have to create object of SecondViewController
,
首先你必须创建对象SecondViewController
,
let objVC: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
Or
或者
let objVC: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
Or
或者
let objVC: SecondViewController? = SecondViewController()
Then add Navigation to 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
回答by Arpit Jain
var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")
var aObjNavi = UINavigationController(rootViewController: objVC)
Now, instead of using view controller object use navigation controller object.
现在,不要使用视图控制器对象,而是使用导航控制器对象。