1 Replies - 4285 Views - Last Post: 30 October 2013 - 01:11 PM

#1 the_sk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 13

How do I run a method from another class in objective C

Posted 22 October 2013 - 02:03 PM

Hey I'm a complete beginner with objective c and more then a little confused, was hoping someone could help me out- I have created this method in a class called ProgrammersCalc.m that looks like this


- (IBAction )resetDisplay:(id)sender
 { 
[_display setString:@"0"]; 
}



this method should set the "display variable" to zero, which is what I want it to do.

In ProgrammersCalc.h I have included this
 - (IBAction)resetDisplay:(id)sender; 



which I hope is the right code, now I've got another class called QCViewController.m where I have a segmented controller action, I don't need help with that as its other operations are working fine. In here I want to be able to run my resetDisplay method so it will reset it - I have tried this in my segmented control if statement as well as other things.

ProgrammersCalc resetDisplay;

as the code, which I know is wrong and it obviously doesn't work. I cant figure out what is the right syntax. So basically I need help. Cheers in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I run a method from another class in objective C

#2 tony12095  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 109
  • Joined: 23-October 12

Re: How do I run a method from another class in objective C

Posted 30 October 2013 - 01:11 PM

What you want to do is go into your QCViewController.m file, and do this:


#import "QCViewController.h"
#import "ProgrammersCalc.h"
 
@implementation QCViewController 

// wherever you need to access the resetDisplay:(id)sender method, just insert these lines.
ProgrammersCalc *pCalc = [[ProgrammersCalc alloc] init]; // pretty much just creates an object that is associated with the ProgrammersCalc class
[pCalc resetDisplay: //sender goes here]; // this is how you would call a function from another class.
@end



Basically, you are creating an object that holds the pointer to the class you're wanting to use, and then use it to have the compiler find the outside class's function.

Hope this helps!

This post has been edited by tony12095: 30 October 2013 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1