How to override popToRootViewController to append a method? - ios

I subclassed UINavigationController and through its didShow delegate, which is called whenever a new view controller is pushed onto the stack, I update an instance variable called previousViewController (to be able to perform some custom work).
The instance variable:
class SectionNavigationController: UINavigationController {
var previousViewController: UIViewController?
...
The delegate where it is updated:
// nav controller delegate method (did show)
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if currentIndex! > 0 {
previousViewController = navigationController.viewControllers[currentIndex! - 1]
}
}
As a consequence, however, whenever popToRootViewController is called, the view controller underneath the top view controller (previousViewController) is not deinitialized because of this reference. How can I override popToRootViewController in such a way that all it does is append the deinitialization of previousViewController?

check this apple documentation
You resolve strong reference cycles by defining some of the
relationships between classes as weak or unowned references instead of
as strong references.
you should declare your var as weak

Related

NavigationController.topViewController is equal to viewController in navigationController delegate

I am implementing the navigationControllerDelegate func:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
print("callin the Navigation Controller Delgate")
if viewController === self {
print("Calling the Navigation Controller delegate because is self and going to call tapButton")
//want to know who was previously on top of navigation.
}
}
I want to know here which was the viewController that is being removed form the stack since apple docs says that the
viewController
The view controller whose view and navigation item properties are being shown.
This means that this assumption is true:
viewController == navigationController.topViewController
or this one:
viewController == navigationController.visibleViewController
If not then one of this are the the viewControllers that is going to be removed.
It's not cleat for me since the func parameter name is willShow viewController, or is just a fancy name and the will show is the already shown.
So if not how from the delegate I may know which VC is being removed from the Navigation Stack.
It would have been nice if the delegate provided the viewController being removed, but I found it's just straight forward just to keep a reference.
weak var lastRemovedViewController: UIViewController? = nil
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
defer { lastRemovedViewController = viewController }
print("Navigation Removed \(lastRemovedViewController.debugDescription)")
}
yes is the answer, the viewController passed in the delegate func is the same as the topViewController, also is equal to navigationController.visibleViewController. so there is no way to know which was the removed viewController from the stack.

Setting common values for viewControllers in UITabBarController with moreNavigationController

I have 'n' ViewControllers, all taking a common variable 'cv1', I would like to set this cv1 before the controllers are loaded.
Everything is fine till I set only 4 controllers, (i.e) without More tab item. and once I introduce more controllers, delegate "tabBarController:didSelectViewController" doesn't trigger for the modules in UIMoreNavigationController.
So I tried setting the UINavigationController delegate for my TabBarController Class and handled the other module tabItem selection like
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if navigationController != self.moreNavigationController
{
//navigation happening in some other tab Item
return
}
if navigationController.viewControllers.count != 2
{
//The first controller of the more tabItem will be MoreViewController, followed by our desired controller in the navigation stack
return
}
guard let selectedController:MyTabItemViewController = self.getTopViewController(from: viewController) else {
print("***WARNING*** Selected controller doesn't seem to expect info :\(String(describing: selectedViewController))")
return
}
selectedController.sp = self.sp
selectedController.sb = self.sb
}
I am able to get the ViewController instance, but the viewDidLoad method gets called before UINavigationController's willShow method, I would like to set the variables before the viewDidLoad method as the UI structuring and data fetch depends on this variable.

Reloading data when clicking back bar button of an UINavigationController

I have an UINavigationController and it has an UITableViewController as its root controller. I reload the data for the table view in viewWillAppear. When I select an item in the table view it will push an new UIViewController with which the user could edit the data that will be shown in the table view, and then I clicked Back of the navigation, viewWillAppear of the UITableViewController was just not called and the data was not reloaded.
So in this case where could I reload the data for the table view?
Thanks.
The easiest way to do that is to use UINavigationControllerDelegate. There are two methods available:
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated animated: Bool)
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated animated: Bool)
In your UITableViewController(root) you should add UINavigationControllerDelegate and method navigationControllerWillShowViewController will be called when you are going back. Here's an example:
class RootTableViewController: UITableViewController, UINavigationControllerDelegate {
...
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated animated: Bool) {
//Will run
}
...
}
viewWillAppear is called when you hit the back button. Just put a log in there if you don't believe me! Most likely the table is not reloading the way you want it to because there is something wrong with the table datasource.

Pushing to a new Viewcontroller from an inactive Viewcontroller (programatically)

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)
You're attempting to call signOut with ContainerViewController().signOut(). This will create a new ContainerViewController and because you haven't pushed it onto the navigation controller's stack, navigationController is nil. Try just calling self.signOut(). (I'm assuming signOut in a method of ContainerViewController)
Update - delegates
Your delegate property should go in SidePanelViewController. I'll give you and example of how to implement it:
SidePanelViewController:
(Note - the protocol doesn't have to go here but I think it keeps things organised)
#objc protocol SidePanelViewControllerDelegate {
optional func needsSignOut(sender: SidePanelViewController)
}
class SidePanelViewController: UIViewController {
// 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?
// Called when the UIButton is pressed.
func myButtonWasPressed() {
delegate?.needsSignOut?(self)
}
}
ContainerViewController:
class ContainerViewController: UIViewController {
var sidePanel: SidePanelViewController!
// Setup the side panel...
override func viewDidLoad() {
super.viewDidLoad()
sidePanel.delegate = self
}
func signOut() {
// Sign out stuff here.
}
}
// The ContainerViewController needs to conform to the SidePanelViewControllerDelegate
// protocol if we want the delegate to work. (This could have gone in the initial
// class declaration.)
extension ContainerViewController : SidePanelViewControllerDelegate {
func needsSignOut(sender: SidePanelViewController) {
self.signOut()
}
}
Hope that helps.
The problem seems to be that navigationController is nil and you're trying to force unwrap it (as indicated by your error).
One problem I discussed in my other answer.
Another problem may be you haven't added a navigation controller. To do this you need to:
If you're using Storyboards
You need to make sure you've embedded your UINavigationController. After that, when you use navigationController it won't be nil and you'll be able to push your view controller.
When you're on your storyboard:
Also, if you're using storyboards, have you considered using segues to move around instead of calling presentViewController? I've found it makes everything much easier.
If you're not using Storyboards
Have a look at this post: Programatically creating UINavigationController in iOS

How to pass data between two ViewControllers with TabBarController in Swift

I have a tab bar class (that is attached to my tab bar controller), Like so:
class CaptionTabBarController: UITabBarController, UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
var logView = self.viewControllers![2] as CaptionsController
logView.log.append("test working!")
}
override func awakeFromNib() {
self.delegate = self;
}
}
And my receiving viewcontroller is like this:
class CaptionsController: UIViewController {
#IBOutlet weak var captionSearchBar: UISearchBar!
#IBOutlet weak var captionsTitle: UILabel!
var receiveImage:UIImage!
var receiveCategoryText:String!
var log = [String]()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
println(log)
}
}
This works when I'm explicitly setting logView.log in CaptionTabBarController.
The result I get in my output windows is as expected. Each tabbar item I click adds "test working!" to the array.
My question is:
How would I be able to get a value from another viewcontroller class to CaptionsController using the tabBarController method I am employing?
This view is a part of a "child" of the tabbar itself, so I'm assuming it already has an instance. All examples I've found just show this, but not how to get data from another class.
The UIViewController that wants to pass the data can store it on your AppDelegate class. Then the UITabBarController delegate method can pull it off and set properties on the receiving UIViewController.
Also, assuming your app is based on the Tab Controller, your AppDelegate can find it with window?.rootViewController as UITabBarController.

Resources