Im working on a project in swift 3.0 and I use the following mechanism to navigate from one UIViewController to another.
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MP3ViewController") as? MP3ViewController {
viewController.mediaDetails = mp3Details
if let navigator = navigationController {
navigator.pushViewController(viewController, animated: true)
}
}
However when I try the same mechanism in the XIB file it gives me an error
Error: saying 'use of unresolved identifier navigationController'
How can I create a navigationController instance here? bellow Ive shown the code of my XIB class that gives me an error
if let categoriesController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CategoriesViewController") as? CategoriesViewController {
categoriesController.categoryName = trimmedCategortyNameText
if let navigator = navigationController {
navigator.pushViewController(categoriesController, animated: true)
}
}
Consider this sample:
let sampleVC = SampleViewController(nibName: "SampleViewController", bundle: nil)
self.navigationController?.pushViewController(sampleVC, animated: true)
As per your requirement, you need to navigation from UIView to UIViewController. You can achieve this in two way
NavigationController
I will be showing you the second way.
Take a variable of UINavigationController
in AppDelegate
In AppDelegate
var navController : UINavigationController?
Then assign the navigation controller to this variable from where you are navigating to UIView. (Let's say from SampleViewController you are adding UIView, then write)
In ViewController (This is my MainVC)
let appDelegate = UIApplication.shared.delegate! as! AppDelegate
appDelegate.navController = self.navigationController
Then in UIView
class ( SampleView in my case)
let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let sampleVC = SampleViewController(nibName: "SampleViewController", bundle: nil)
appDelegate.navController?.pushViewController(sampleVC, animated: true)
I verified this.
You can do like this:
If you are using storyboard :
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MP3ViewController") as? MP3ViewController {
viewController.mediaDetails = mp3Details
if let navigator = self.navigationController {
navigator.pushViewController(viewController, animated: true)
}else{
let navigation = UINavigationController(rootViewController:viewController)
self.present(navigation, animated: true, completion: nil)
}
}
With Xib
Initialize a instance of UIViewController like this:
let vc = MP3ViewController(nibName:"MP3ViewController",bundle:Bundle.main)
vc.mediaDetails = mp3Details
if let navigator = self.navigationController {
navigator.pushViewController(viewController, animated: true)
}else{
let navigation = UINavigationController(rootViewController:viewController)
self.present(navigation, animated: true, completion: nil)
}
You can use the same mechanism with Xib using Appdelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let currentOrderVC = storyboard.instantiateViewController(withIdentifier: "yourVcId") as! YourVC
var nav1 = UINavigationController()
nav1.viewControllers = [currentOrderVC]
UIApplication.shared.keyWindow?.rootViewController
UIApplication.shared.keyWindow?.rootViewController = nav1
UIApplication.shared.keyWindow?.makeKeyAndVisible();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.