Short explanation.
I have a ContainerViewController
that I'm pushing to the navigationStack.
The ContainerViewController
has 2 child ViewControllers. A SlidePanelViewController
(a slide-out menu) and a CenterViewController
(the content)
I have a button in my menu to "sign Out". When this button is clicked I want to push ContainerViewController
(and it's 2 childViewControllers
) to my LandingPageViewController
.
Here's the function I am trying to call:
func signOut() {
println("signOut")
// Set up the landing page as the main viewcontroller again.
let mainTableViewController = LandingPageVC()
mainTableViewController.navigationItem.setHidesBackButton(true, animated: false)
mainTableViewController.skipView = false
self.navigationController!.pushViewController(mainTableViewController, animated: true)
// Disable menu access
menuEnabled = false
// change status bar style back to default (black)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
}
At first I tried putting this in my SlidePanelViewController
. That didn't work. So I put it where I'm assuming it belongs in the ContainerViewController
.
However when I click my signOutButton
in my menu. I'm presented with the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
When looking into the error. This is the line causing it:
self.navigationController!.pushViewController(mainTableViewController, animated: true)
After the error I checked that the function works, by adding a UINavigationBarButtonItem
that called the function (in my ContainerViewController
). It did exactly what I wanted.
However when I call this function from my Menu (again my menu is a childViewController
of the ContainerViewController
). It does not work.
I'm attempting to call it like so:
ContainerViewController().signOut()
I also tried adding a Delegate
to my SidePanelViewController
like this:
Before the class:
@objc protocol SidePanelViewControllerDelegate {
optional func needsSignOut(sender: SidePanelViewController)
optional func toggleLeftPanel()
optional func collapseSidePanels()
}
in viewDidLoad()
:
// Make sure your delegate is weak because if a ContainerViewController owns
// a reference to a SidePanelViewController and the container view controller
// is its delegate, you'll end up with a strong reference cycle!
weak var delegate: SidePanelViewControllerDelegate?
in my tap gesture function:
func signOutTapGesture() {
println("signOutTapGesture")
selectView(signOutView)
delegate?.needsSignOut?(self)
println(delegate)
}
before my ContainerViewController
class:
var leftViewController: SidePanelViewController?
my ContainerViewController
class:
class ContainerViewController: UIViewController, CenterViewControllerDelegate, SidePanelViewControllerDelegate, UIGestureRecognizerDelegate {
in my ContainerViewController's
viewDidLoad()
leftViewController?.delegate = self
And I changed the signOut
function in the ContainerViewController
class to this:
func needsSignOut(sender: SidePanelViewController) {
println("needsSignOut called")
self.signOut()
}
However using the delegate like above, doesn't seem to do anything either.
Any help as to How I can successfully push my LandingPageVC from the menu would be greatly appreciated! (I'm not using storyboards)