SWRevealViewController
SWRevealViewController copied to clipboard
Front view always create new ViewController not use previous View Controller
assume as
- SUB MENU 1 is FirstViewController
- SUB MENU 2 is SecondViewController
when I tap SUB MENU 2, FirstViewController will deinit and when I tab SUB MENU 1 back, SecondViewController will deinit and FristViewController will create new.
My question is How to use old ViewController such as when I tap SUB MENU 2 and back to SUB MENU 1, FirstViewController will show old data or previous state
Just by keeping their reference in some properties.
How to keeping it My code is here `override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let index = indexPath.row
switch index {
case 1:
if let controller = self.storyboard?.instantiateViewController(withIdentifier: "Nav1") as? FirstNav{
revealViewController().pushFrontViewController(controller, animated:true)
if let vc = controller.viewControllers.first as? FirstViewController{
vc.txtTitle = "Hello My Second Time"
}
}
break
case 2:
if let controller = self.storyboard?.instantiateViewController(withIdentifier: "Nav2") as? SecondNav{
revealViewController().pushFrontViewController(controller, animated:true)
}
break
default:
break
}
}`
Could you upload a sample project, I will modify it...
Here is my example Sidebar.zip
@iDevelopper ok It works perfectly, thank you 👍
This does not seem to work for me. Going through the code, everything seems as it should be, but it still recreates a new view controller every time you select an option, which is very bad, and possibly fatal for my specific app. Here is my TableViewController (or as I call it MenuController) code:
class MenuController: UITableViewController {
var firstController: HomeNav?
var secondController: SettingsNav?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.bounds.size.width = 275
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let index = indexPath.row
print(index)
switch index {
case 1:
if self.firstController != nil {
revealViewController().pushFrontViewController(self.firstController, animated:true)
}
else if let controller = self.storyboard?.instantiateViewController(withIdentifier: "homeNav") as? HomeNav{
revealViewController().pushFrontViewController(controller, animated:true)
}
break
case 7: //This is the row of the label for the view controller
if self.secondController != nil {
revealViewController().pushFrontViewController(self.secondController, animated:true)
}
else if let controller = self.storyboard?.instantiateViewController(withIdentifier: "settingsNav") as? SettingsNav {
self.secondController = controller
revealViewController().pushFrontViewController(controller, animated:true)
}
break
default:
break
}
}
Here is the code for HomeNav (my first view controller):
class HomeNav: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let menuController = self.revealViewController().rearViewController as? MenuController
menuController?.firstController = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the code for SettingsNav (my second view controller):
class SettingsNav: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let menuController = self.revealViewController().rearViewController as? MenuController
menuController?.secondController = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In case you should need it, I need to say that I cannot post my entire project for fear of thievery; however, I can post little bits of code here and there if needed, as well as interface builder screenshots. Thank you for your time.
Your firstController and secondController are surely nil.
If I understand it seems that your menuController is embedded in a navigation controller, then:
class HomeNav: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let navigationController = self.revealViewController().rearViewController as? UINavigationController
let menuController = navigationController?.topViewController as? MenuController
menuController?.firstController = self
}
}
Same for SettingsNav.
Hop it helps!
This worked! Thank you so much! I completely forgot to mention that my MenuController was embedded in its own NavigationController.