SWRevealViewController pushing back to front view controller showing error - ios

I am using SWRevealViewController for preparing a slide menu. On clicking WishList cell in my side bar it moves to its corresponding VC. I have my front view controller with storyboardID - "LandingPage". Now when I move to the WishList page I have a back button pressing on which user returns to the already set front view controller. I am using the following approaches but without any success.
1.Using normal presentViewController:() method
#IBAction func backButton(sender: AnyObject)
{
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LandingPage")
self.presentViewController(vc!, animated: true, completion: nil)
}
}
But this results in an error in viewDidLoad() in LandingPageVC as -
and error message reads - "fatal error: unexpectedly found nil while unwrapping an Optional value"
2.By setting a navigation controller and front VC.
#IBAction func backButton(sender: AnyObject)
{
if let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LandingPage") as? LandingPageViewController
{
let navController = UINavigationController(rootViewController: vc)
navController.setViewControllers([vc], animated:true)
self.revealViewController().setFrontViewController(navController, animated: true)
}
}
Here I get the following error -
and error message reads - "fatal error: unexpectedly found nil while unwrapping an Optional value"
So what mistake am I doing in my approach and how to move to the already set front VC ? Thanks in advance.

Related

UINavigationController is nil in swift 3

In my main storyboard I have 1 Navigation controller and subsequent swift file NavigationCtr.swift.
The viewController is in different xib.
Now I want to push my viewController from the NavigationCtr class.
let vcFirst = FirstViewController(nibName: "FirstViewController", bundle: nil)
self.navigationController!.pushViewController(vcFirst, animated: true)
I am getting a exception
fatal error: unexpectedly found nil while unwrapping an Optional value
So when I trying to print from viewDidLoad
print(self.navigationController)
in NavigationCtr.swift class it is giving nil. So nothing works
I created a new project in objective C and it works fine.
Attached the storyboard image
Any hint in the direct direction is highly appreciated.
Note : - I am new to swift
the main issue was using the self.navigationController
As my storyboard only had navigationController I should just self keyword.
as below
let vcFirst = FirstViewController(nibName: "FirstViewController", bundle: nil)
self.pushViewController(vcFirst, animated: true)
For the first controller, set it as rootController instead of push (show).
self.navigationController = UINavigationController(rootViewController: vcFirst)
Or set its view controllers
self.navigationController = UINavigationController()
navigationController.setViewControllers([vcfirst], animated: true)

SWRevealViewController - reveal it manually under #IBAction

This is my StartViewController with simple #IBAction:
class StartViewController: UIViewController {
#IBAction func startButtonTapped(sender: UIButton) {
revealViewController().revealToggleAnimated(true) //error
}
}
Along with the commented line there is an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
What am I doing wrong?
Of course StartViewController is presented from SWRevealViewController via sw_front segue.
For some reason revealViewController() returns nil here. Why?
This is how my storyboard looks like:
When you implementing SWRevealViewController using storyboard, you should set Storyboard Entry Point to SWRevealViewController instance (Reveal View Controller on picture), not to front view controller (Start View Controller on picture).
Note that revealViewController method return optional pointer, and even when all is set properly it return nil until view is loaded, so you better use optional chaining:
revealViewController()?.revealToggleAnimated(true)
I think your app might crash because you are missing a delegate.Try to add the following to your protocols:
class StartViewController: UIViewController,SWRevealViewControllerDelegate {
#IBAction func startButtonTapped(sender: UIButton) {
revealViewController().revealToggleAnimated(true) //error
}
}
If it still crashes, try changing the revealViewController().revealToggleAnimated(true) with revealViewController().revealToggle(self) and see if it still crashes.

From ViewController navigate back to SWRevealViewController 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.
You need to dismiss the presented viewController:
#IBAction func btnBack(sender: AnyObject)
{
let home = HomeTableViewController()
self.dismissViewControllerAnimated(true, completion:nil);
}
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);
}
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.

Transition without loose data with Storyboard and Swift

I'm developing a new project in Swift but i've a big (and dumb) problem.
I've three viewControllers:
1.- Call webservices, take JSON and pass this JSON to the second view
2.- Parsing data and put the data in the view (tableview and everything ok). At this view I go to other view with a button (to the view 3). I don't need pass nothing.
3.- I try to go back to view 2 but the view is empty because the data who posicioning at the tableview was pass from view 1.
I try with a lot of solutions:
navigationController!.dismissViewControllerAnimated(false, completion: nil) -> If i link the storyboard with a transition from view 3 to view 2, the view 2 is empty, if i don't link in storyboard -> unexpectedly found nil while unwrapping an Optional value
If i change "!" for "?" do nothing
The same with every code i have been test.
My code is:
View 1 to view 2: (connected in the visual part of Storyboard (IB))
#IBAction func goRecibosPendientes(sender: UIButton) {
let controllerVC = self.storyboard!.instantiateViewControllerWithIdentifier("recibos") as! TablaRecibosTableViewController
controllerVC.initWithJSON(self.jsonReceived)
self.presentViewController(controllerVC, animated: true, completion: nil)
}
View 2 to View 3: (connected in the visual part of Storyboard (IB))
#IBAction func goToTablaProveedoresDeServicios(sender: UIButton) {
let controllerVC = self.storyboard!.instantiateViewControllerWithIdentifier("proveedoresDeServicios") as! TablaProveedoresDeServicios
self.presentViewController(controllerVC, animated: true, completion: nil)
}
View 3 to 2: I try every kinds of do that (every kinds i know, not EVERY kinds)
#IBAction func backPressed(sender: UIButton) {
navigationController!.dismissViewControllerAnimated(false, completion: nil)
navigationController!.dismissViewControllerAnimated(true, completion: nil)
navigationController!.popToRootViewControllerAnimated(true)
}
Thanks a lot.
If you are using NavigationController than try to push a view controller it will remains your previous view in stack.
self.navigationController.pushViewController(controllerVC, animated: true)

How to dismiss current view and reload another?

#IBAction func DoneButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as UIViewController
viewController.viewWillAppear()
}
Its suppose to dismiss my current view and reload ViewController
but it crashes with fatal error: unexpectedly
found nil while unwrapping an Optional value
is it because I'm dismissing before?
The call to instantiateViewControllerWithIdentifier() returns an optional. So if the view controller with the specified identifier cannot be found in your storyboard, it will be set to nil and the next line where you call viewWillAppear will crash.
It is also possible that you crash inside viewWillAppear actually. That is not a method that you should call. Instead, UIKit will call that method for you when your view controller is presented.
I guess what you are trying to do is something like this:
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as UIViewController {
self.presentViewController(viewController,
animated: true, completion: nil)
}
If not, provide more context to your question.
I was thinking to do either
1.
let viewController = ViewController()
viewController.viewWillAppear(false)
2.
In secondary class:
let viewController = ViewController()
viewController.shouldRefresh = true
In primary class:
var shouldRefresh: Bool!
override func viewWillAppear(animated: Bool) {
if shouldRefresh == true {
}
}
3.
In secondary class:
let viewController = ViewController()
viewController.reloadRoutineData()
In primary class:
func reloadRoutineData() {
// Do my stuff here
}
The third option is my variable because I only need to do certain code, not really reload the view completely. But all of them crash with nil while unwrapping.

Resources