如何使用 Swift 在 ios 中使用导航控制器导航应用程序的所有视图(我使用的是 Xib 而不是 Storyboard) [英] How to Navigate all views of application using Navigation Controller in ios using Swift (I am using Xib not Storyboard)
问题描述
(我搜索但找不到任何可行的解决方案)
(I searched but could not find any working solution)
我创建了 5 个 Xib,它们代表我的应用程序的不同屏幕.现在我想在我的应用程序中添加导航,以便用户可以来回移动.我还想在视图控制器之间传递数据.我以前做过这个,使用简单的 Storyboard.但我不知道如何用 Xib 做到这一点.
I have created 5 Xib's which are representing different screens of my application. Now I want to add navigation in my application so that user can go back and forth. I also want to pass data between view controllers. I have done this before, usinig Storyboard that was simple. But i do not have any idea how to do it with Xib.
我使用的是 xcode 7.3 和 swift 2.0
I am using xcode 7.3 and swift 2.0
非常感谢正确的指导.
推荐答案
试试这个.或许能帮到你.
Try this. It may help you.
AppDelegate.swift
AppDelegate.swift
var window: UIWindow?
var nav:UINavigationController!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
let FirstVC = HomeViewController(nibName: "HomeViewController", bundle: nil) as HomeViewController
let nav = UINavigationController(rootViewController: FirstVC)
nav.navigationBarHidden = true
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
然后当你想推送另一个视图控制器时,简单地使用以下代码移动到另一个视图控制器.
Then when you want to push the other view controller , simple use following code to move to another view controller.
@IBAction func pushHomeVC(sender: UIButton)
{
let homeVC = HomeViewController(nibName: "HomeViewController", bundle: nil) as HomeViewController
// If you want to data pass in Second Screen.
homeVC.strData = "Pass data that you want"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.nav.pushViewController(homeVC, animated: true)
}
这篇关于如何使用 Swift 在 ios 中使用导航控制器导航应用程序的所有视图(我使用的是 Xib 而不是 Storyboard)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!