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 中实例化没有故事板的导航控制器
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
