-2

I am new to ios.I have a class which carries all attributes and methods.I am intending to access that class content one json method which parses the data.Now, I have another class. I want to call json method in that class.Plz tell how can i achieve this.

Saba Sayed
  • 181
  • 2
  • 4
  • 11

5 Answers5

4

If the method is class (i.e. static) method:

[NameOfClass methodName:parameter];

If the method is instance method:

[instanceOfClass methodName:parameter];
Lukman
  • 18,462
  • 6
  • 56
  • 66
0

Make you method public. to Do so add declaration of you method to .h file.

Like so:

-(void) myJSONMethod;
Eugene P
  • 554
  • 4
  • 19
0

here is reference Accessing Method from other Classes Objective-C

@implementation commonClass
+ (void)CommonMethod:(id)sender  /* note the + sign */
{
//So some awesome generic stuff...
    }
@end

@implementation ViewController2

- (void)do_something... {
    [commonClass CommonMethod];
}


@end

Option 2:

@implementation commonClass
- (void)CommonMethod:(id)sender
{
//So some awesome generic stuff...
    }
@end

@implementation ViewController2

- (void)do_something... {
    commonClass *c=[[commonClass alloc] init];
    [c CommonMethod];
    [c release];
}

@end

Option 3: use inheritance (see Mr. Totland's description in this thread)

@implementation commonClass
- (void)CommonMethod:(id)sender
{
//So some awesome generic stuff...
    }
@end

/* in your .h file */

@interface ViewController2: commonClass

@end

naturally you always need to #import commonClass.h in your view controllers..

Community
  • 1
  • 1
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
0

You need to create an object for your second class. By using this object, you can call methods that you declared in the second class header file.

ClassName *obj = [[ClassName alloc]init];
[obj methodName];
manujmv
  • 6,450
  • 1
  • 22
  • 35
0

if you have to call a class method

[ClassName methodName:parameter];

if you have instance method

 ClassName * obj = [[ClassName alloc] init];

 [obj methodName:parameter];

but make sure that , you have declared that method in ClassName.h file also .

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70