0

Navigation from view controller back to SWReveal Front Page

As you can see in the picture, there're 3 controllers at the bottom which are (HomeTableViewController, NavigationViewController and NewsViewController)

HomeTableViewController is the Main Page which I'm using the SWRevealViewController and set it as a front page. (sw_front)

After I've selected a row in HomeTableViewController, it can navigate to NewsTableViewController. However, I've added a back button in the NewsViewController to navigate back to the previous page which is HomeTableViewController, I'm not manage to do that with this code.

In the HomeTableViewController navigate to NewsViewController by selected a row

@IBAction func btnBack(sender: AnyObject)
{
    let home = HomeTableViewController()
    self.presentViewController(home, animated: true, completion: nil)
}

In the NewsViewController press back button back to previous page

@IBAction func btnBack(sender: AnyObject)
{
    let home = HomeTableViewController()
    self.presentViewController(home, animated: true, completion: nil)
}

If I pressed the back button in the NewsViewController, this error appeared.

fatal error: unexpectedly found nil while unwrapping an Optional value

self.revealViewController().rearViewRevealWidth = 200

This code has an error and is located in the HomeTableViewController. I hope that someone could help me on this. Thank you.

jefferyleo
  • 630
  • 3
  • 17
  • 34

3 Answers3

0

You need to dismiss the presented viewController:

@IBAction func btnBack(sender: AnyObject)
{
    let home = HomeTableViewController()
    self.dismissViewControllerAnimated(true, completion:nil);
}
Islam
  • 3,654
  • 3
  • 30
  • 40
  • I still having the same problem, it shows the same error said that (fatal error: unexpectedly found nil while unwrapping an Optional value self.revealViewController().rearViewRevealWidth = 200) Because the current view controller is not swrevealviewcontroller, and I need to back to sw_front, I couldn't do that – jefferyleo Nov 30 '15 at 09:06
0

You are getting this error because you are creating new HomeTableViewController in btnBack (and this new controller probably doesn't have revealViewController). There is no need to do this. You can just call self.dismissViewControllerAnimated(true, completion:{}); as Islam Q. suggested:

@IBAction func btnBack(sender: AnyObject)
{
    self.dismissViewControllerAnimated(true, completion:nil);
}
grandboum
  • 622
  • 4
  • 5
  • I've delete the previous code and add your code in it, it doesn't have any respond, does I need to create any segue from NewsViewController to HomeTableViewController? – jefferyleo Nov 30 '15 at 09:18
  • No, you should be ok without segue. Is there any chance that NewsViewController is pushed (via `self.navigationController?.pushViewController(...)`)? If so, than you need to pop it `self.navigationController?.popToRootViewControllerAnimated(true)` – grandboum Nov 30 '15 at 09:28
  • I cannot use pushViewController since I want to navigate back to TableView, so tableView is not applicable in this function. I've tried popToRootViewControllerAnimated, it cannot work as well, it has no any respond after I've clicked the back button. – jefferyleo Nov 30 '15 at 09:37
  • Hm, ok, that you do need an unwind segue. Please see this answer as example on how to do it http://stackoverflow.com/a/15839298/973153 – grandboum Nov 30 '15 at 10:04
  • I'll try that on tomorrow because I've left my office. – jefferyleo Nov 30 '15 at 12:25
0

https://github.com/John-Lluch/SWRevealViewController/issues/516#issuecomment-160590440 This solve my issue. Delete the navigation controller between the Home and News Controller, and use the push Segue from Home to NewsViewController.

jefferyleo
  • 630
  • 3
  • 17
  • 34