-3

I have some variable lets say isMenuVisible = false; I want to set some function when this var is changed:

isMenuVisible: Bool!{
     didSet{
        callFunctionFromOtherViewController()
    }
}

How is that possible? Do I need to create instance of VC in order to access that function? Or I need to make that function public?

Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
Yestay Muratov
  • 1,338
  • 2
  • 15
  • 28
  • your class that wants to call the method needs to have either an instance of the other class or other means to reach it, e.g. via AppDelegate and the callable method needs to be public but they are internal by default which means they are public to other classes in the same app domain. – BadmintonCat Jun 01 '15 at 11:12

3 Answers3

0

To call a method/function from another VC (or from any other class), you have 2 choices:

  1. Create a class method in this other view controller:

In your MyViewController.h

+(void)myClassMethod;

Wherever you need to use it

[MyViewController myClassMethod];
  1. Create an instance method in this other view controller:

In your MyViewController.h

-(void)myClassMethod;

Wherever you need to use it

MyViewController *myViewControllerInstance = [[MyViewController alloc] init];  
[myViewControllerInstance myClassMethod];
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
0

definitely no need to create instance for the viewController, if it is a rootViewController you can get it like that :

var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var vc = (appDelegate.window?.rootViewController as? MyViewController)!

if your view controller is parent, it is like that:

var myViewfromParent = self.parentViewController as? MyViewController

if you use storyboard with id, you can get it like that:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController

hope it helps

Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
0

If you're using viewController containment and you can easily reference the view controller you can simply create a public method in your .h file and call that method... however, with viewControllers it's often the case that a. you don't have / want universal access to the viewController instance in other classes and b. the viewController may not even exist when this method is changed. For these reasons I'd go with NSNotifications when triggering actions in viewControllers.

NSNotificationCenter addObserver in Swift

If this isn't acceptable and you can guarantee that this class will be linked in an architecturally sound way, I would create a delegate. That way you can set the delegate when you create the viewController and there's no mess when trying to get back to your viewController... I'd only recommend this if there's a genuine connection between the viewController and the class that contains the action (in this case the bool)

In objective c you'd do

MyViewController *myViewController = [[MyViewController alloc] init];
myViewController.delegate = theClassContainingTheBool; // could be self.. might not be... If you can't do this line of code, then you should use notifications

In swift

Delegates in swift?

How to define optional methods in Swift protocol?

Community
  • 1
  • 1
Magoo
  • 2,552
  • 1
  • 23
  • 43