Swift – Instantiating a navigation controller without storyboards in App Delegate(Swift – 在 App Delegate 中实例化没有故事板的导航控制器)
问题描述
我正在重建一个没有情节提要的应用程序,其中我遇到最大问题的部分是以编程方式导航视图到视图.很少有不使用故事板的东西写出来,因此很难找到答案.
I'm rebuilding an app without storyboards and the part of it that I'm having the most trouble with is navigating view-to-view programatically. Few things are written out there which don't use storyboards, so finding an answer for this has been tough.
我的问题很简单.我有我的 ViewController
和我的 SecondViewController
,我想从前者推送到后者.
My problem is pretty simple. I have my ViewController
and my SecondViewController
and I want to push from the former to the latter.
在AppDelegate
中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor = UIColor.whiteColor()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
然后在ViewController.swift
中:
class ViewController: UIViewController, AVAudioPlayerDelegate, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
startFinishButton.setTitle("Begin", forState: .Normal)
startFinishButton.addTarget(self, action: "moveToSecondViewController", forControlEvents: .TouchUpInside)
view.addSubview <*> startFinishButton
}
func moveToSecondViewController(sender: UIButton) {
let vc = SecondViewController()
println(self.navigationController) // returns nil
self.navigationController?.pushViewController(vc, animated: true)
}
}
打印 self.navigationController
返回 nil.我试过这样做:
Printing self.navigationController
returns nil. I've tried doing:
var navController = UINavigationController()
当 ViewController
类被创建(但在 ViewDidLoad 之外,就在类声明下方)并使用 完成推送时navController
var 但没有奏效.
var navController = UINavigationController()
when the ViewController
class is created (but outside of ViewDidLoad, right under the class declaration) and done the push using the navController
var but that hasn't worked.
我在想也许解决方案是在 App Delegate 中创建一个导航控制器,整个应用程序都会使用它,我猜它是一个全局变量?
I'm thinking maybe the solution is to create a navigation controller in App Delegate that the whole app would use, I guess as a global variable?
我希望这篇文章可以为许多刚接触 Swift 并希望从他们的应用程序中删除故事板的人提供帮助.
My hope is that this post can serve many others who are new to Swift and want to remove storyboards from their app.
感谢您的浏览和帮助.
推荐答案
在 Swift 3 中
将此代码放在 AppDelegate 类的 didFinishLaunchingWithOptions 方法中.
Place this code inside didFinishLaunchingWithOptions method in AppDelegate class.
window = UIWindow(frame: UIScreen.main.bounds)
let mainController = MainViewController() as UIViewController
let navigationController = UINavigationController(rootViewController: mainController)
navigationController.navigationBar.isTranslucent = false
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
这篇关于Swift – 在 App Delegate 中实例化没有故事板的导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift – 在 App Delegate 中实例化没有故事板的导航控制器
基础教程推荐
- UINavigationBar 隐藏按钮文本 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01