0

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.

Michael Nana
  • 1,969
  • 4
  • 24
  • 36

2 Answers2

1

Just call showMe on whatever instance of HeyViewController that you've already instantiated.

NRitH
  • 13,441
  • 4
  • 41
  • 44
  • Sorry, I probably didn't explain properly. The current view controller doesn't have an instance to that view controller. It was initialized because to get to the current view controller you passed through the target view controller. – Michael Nana Sep 23 '13 at 23:56
  • View controllers don't "have an instance to [some other] view controller." They exist, or not. The problem is getting a pointer to an existing view controller. – Duncan C Sep 24 '13 at 00:17
  • If the VC you're trying to access is the *parent* of the one you're accessing it from, then you can simply get your current VC's `parentViewContoller` property. If not, you may need to start at the root controller and traverse your way through the controller hierarchy to find the one you want. Take a look at [this question](http://stackoverflow.com/questions/6131205/iphone-how-to-find-topmost-view-controller) for a general idea. – NRitH Sep 24 '13 at 06:31
1

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Your last paragraph is the fastest guide to the "how to make a singleton" question.+1 – gWiz Sep 24 '13 at 00:47