Swift – 在App Delegate中实例化没有故事板的导航控制器
我正在重build一个没有故事板的应用程序,而我遇到的最麻烦的部分是以编程方式导航到视图。 很less有东西写在那里不使用故事板,所以find答案是艰难的。
我的问题很简单。 我有我的ViewController
和我的SecondViewController
,我想从前者推到后者。
在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。 我试过了:
var navController = UINavigationController()
当ViewController
类被创build(但在ViewDidLoad之外,正好在类声明下)并且使用navController
var进行了推送,但是没有工作。
我想也许解决的办法是在App Delegate中创build一个导航控制器,整个应用程序将使用,我想作为一个全局variables?
我希望这篇文章可以服务于许多其他Swift新手,并希望从应用程序中删除故事板。
感谢您的帮助。
在Swift 3
将此代码放在AppDelegate类的didFinishLaunchingWithOptions方法内。
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()
在AppDelegate中
var window: UIWindow? var navController: UINavigationController? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { navController = UINavigationController() var viewController: ViewController = ViewController() self.navController!.pushViewController(viewController, animated: false) self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.rootViewController = navController self.window!.backgroundColor = UIColor.whiteColor() self.window!.makeKeyAndVisible() return true }
在ViewController中
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.title = "FirstVC" var startFinishButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton startFinishButton.frame = CGRectMake(100, 100, 100, 50) startFinishButton.backgroundColor = UIColor.greenColor() startFinishButton.setTitle("Test Button", forState: UIControlState.Normal) startFinishButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(startFinishButton) } func buttonAction(sender:UIButton!) { println("Button tapped") let vc = SecondViewController() self.navigationController?.pushViewController(vc, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
- 我如何连接UITableView到我的Appdelegate.m?
- 如何将数据从AppDelegate传递给ViewController?
- iPhone registerForRemoteNotificationTypes不会产生错误,但不会触发授予设备令牌的委托
- 第一次启动应用后更改初始视图控制器
- 为什么我得到延迟加载NSBundle MobileCoreServices.framework?
- 其视图不在窗口层次结构中 – 电子邮件表单
- 从AppDelegate调用GameScene方法(Swift 3,SpriteKit,Xcode 8)
- iOS:如何在X小时内每隔一小时在睡眠模式下自动发送电子邮件与iPad?
- 从AppDelegate呈现UIAlertController