0

I would like to change the text of my UIBarButtonItem from another class (objective-C++) that I use in my project.

I have an IBOutlet myButton setup in myViewController and I can successfully do something like:

[ myButton setTitle:@"newTitle" ];

in myViewController.mm

Now I would like to do the same but from myCppClass that I use in my project.

Is there a way for myCppClass to access myViewController's myButton? Shall I use some type of delegation mechanism?

I am pretty new to Ios and objective-C.

Thanks,

Baba

Baba
  • 475
  • 6
  • 19
  • Is there any reason you can't change the c++ class to objective c++? That should allow you to call the method directly. If not, see the accepted answer to this question: http://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-method for a great explanation of creating a c/c++ wrapper for objective c functions. – Jeames Bone Dec 05 '13 at 21:59
  • The class is actually objective c++. Sorry I wasn't very clear. I edited my question to reflect that. How would I "call the method directly" since it belongs to another class? – Baba Dec 05 '13 at 22:05

3 Answers3

1

The short answer is, don't. You should treat a view controller's views as private. Instead, add a method to your VC like changeButtonTitle. Then call that method from from your other class.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks. I do not know how to "call that method from my other class". How do I let my other class know about my first class's method? – Baba Dec 05 '13 at 22:09
1

Create a method within your myViewController class to change the button title, then call that method from myCppClass by following the instructions described in this answer:

How to call method from one class in another (iOS) https://stackoverflow.com/a/9731162/2274694

Community
  • 1
  • 1
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
0

The answers above are correct, but from your comments I suspect you aren't yet happy.

If you are super lazy, and you don't mind the string being the same in all instances of the VC (which in practice is usually the case) you could simply write a getter and setter for the string name as a class variable in the destination class. That way you don't even need access to the actual instance of the class, just its name. Tacky but super easy.

As others have pointed out, don't try and modify the buttons on a different VC directly. Pass a message and have the owning VC do it when it loads.

Well, passing messages forwards (to a new VC) is very easy. At the bottom of every VC class code there is #pragma navigation section commented out which gives you a handle to the destination VC. Cast it to the proper type and you set properties in the destination VC instance. In your case, create a public property NSString which holds the button text in your destination VC, and set it in your navigation section. This could be any class, or even a delegate, but a simple string should work.

Passing messages backwards (to previous VCs) can work the same way but it starts to get messy. You can programatically step back through the stack of VCs to find a particular (instance of) a VC. One of the answers to Get to UIViewController from UIView? gives sample code for stepping back through view controllers.

But if its simply forward communication, passing messages or information through

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

at the bottom of the VC code and the commented out lines below is very easy and safe.

Community
  • 1
  • 1
Peter Webb
  • 671
  • 4
  • 14