How to Navigate all views of application using Nav

2019-09-09 05:25发布

问题:

(I searched but could not find any working solution)

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.

I am using xcode 7.3 and swift 2.0

Proper guidelines will be highly appreciated thanks.

回答1:

Try this. It may help you.

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) 
}