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 中实例化没有故事板的导航控制器
基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
