0

I have an instance of a class that handles and implements swipe navigation between my view controllers. I have a method in that class that allows me to simulate a swipe navigation to another view controller.

In a perfect scenario I would add a button to a view controller in the IB and in its action call that method of the handler class in order to navigate to the other view controller.

It seems like what I'm trying recreates an instance and obviously doesn't work because it doesn't have all the info the other instance has.

I also tried adding a subview of a button to my view controller from the handler class itself and that worked but I wanted to know if there was a prettier way.

Maybe access the already existing instance of the class from my other view controller in order to call the method?

I've tried swipe().navigateToLeftVC() and that recreates instance.

3
  • I just noticed that the initial instance is created in the app delegate and assigned to the window.rootviewcontroller. Would this be of any assistance? Thanks again Commented Mar 20, 2016 at 13:08
  • 1
    Maybe you could create a global variable that has the instance value like this: 'let myInstance = myClass()' and then refer to that instance whenever you need to call your function. You can refer to that instance anywhere since it's defined in global scope. Also, maybe create a singleton instead of a global variable. Commented Mar 20, 2016 at 13:11
  • You can also pass the instance to each view controller Commented Mar 20, 2016 at 13:20

1 Answer 1

1

I solved this by creating a singleton.

Added this code to my controller

class var sharedManager: controllerName {
        struct Static {
            static let instance = controllerName()
        }
        return Static.instance
    }

Then in my app delegate where I assigned it to the root view controller I used controllerName.sharedManager which returned my singleton instance.

Then in my other view controller when I wanted to call a method of that class I used controllerName.sharedManager.methodName().

Works perfectly.

Sign up to request clarification or add additional context in comments.

7 Comments

While this might work fine in this application. You really should avoid making unnecessary instances of your View Controllers. With this approach, you now have two versions of your controllerName() class out there. This can cause unexpected problems later down the line. I have a few possible alternatives for you. Are you using a navigation controller?
Indeed the word perfectly is a stretch here. This isn't a perfect solution by any means. It may work, sure. But in reality, this isn't the ideal solution. We'd need to see more of what's going on in your actual code to understand why you even feel this is necessary.
Thank you guys for sharing your knowledge. @nhgrif : I have a class (EZSwipeController) that implements side swiping through your view controllers. It comes with a navigation controller implementation which I am not using as it does not fit with my application. So, I needed a way to be able to have buttons in my view controllers that called methods of the EZSwipeController library in order to having an alternate to swiping the view controllers and be able to navigate through the button action. Can you explain how I have two versions of controllerName if I'm only instantiating 1?
@Walking If you're really interested in talking about the best way to do things, I recommend you check out Code Review. This problem isn't really going to be solvable through a chain of comments.
@Walking When I stated that you'd have two instances of your controller I was assuming you've already got an instance of that controller assigned to a controller in your storyboard. In the future, make sure you share more code to avoid confusion! Also, check out How to Ask.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.