Reputation: 1977
I'm trying to call a method from another viewcontroller say HeyViewController that is already initialized. How would I call a method of it with re initializing a new instance of that view controller?
e.g HeyViewController *hey =[[HeyViewController alloc]init]; [hey showMe];
is no good because that initializes the view controller when it's already initialized.
So yeah, how would I do this?
EDIT:
Sorry it's unclear. The viewcontroller in question has been initialized before this viewcontroller due to the flow of the app using storyboard. So the viewcontroller is running but an instance of that viewcontroller is not in the current view controller.
Upvotes: 0
Views: 915
Reputation: 131408
The problem you are facing is how do you get a pointer to an existing view controller. In order to answer that, you need to think about how that other view controller gets created.
What is your flow of view controllers? Who creates your "Hey" view controller? Does your view controller who needs access to the "Hey" view controller have access to the view controller that created the "Hey" view controller?
If the answer is yes, then have the view controller that creates the "Hey" view controller save a pointer to it in it's prepareForSegue method. Then ask the creating view controller for a pointer to the "Hey" view controller when you need it.
If your third view controller does not have access to the creator of the "Hey" view controller, will there only ever be 1 instance of your "Hey" view controller?
If you can be sure you will only ever have one instance of your "Hey" view controller, then add a "currentHeyViewController" class method to your "Hey" view controller. Add a private global "gCurrentHeyVC" to the header of the "Hey" VC. Return the value of the global in your currentHeyViewController class method. Set the global in your init methods, and nil the global in your dealloc method.
Edit: As @gWiz pointed out below, the last option above is a quick descriptiono of how to turn the "Hey" view controller into a singleton.
Upvotes: 1
Reputation: 13893
Just call showMe on whatever instance of HeyViewController that you've already instantiated.
Upvotes: 1