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.
Asked
Active
Viewed 115 times
-2
-
Have you tried to google your question? – Tarek Hallak Nov 27 '13 at 08:13
-
yes didnt get the right answer – Saba Sayed Nov 27 '13 at 08:21
-
http://stackoverflow.com/questions/9731126/how-to-call-method-from-one-class-in-another-ios and you may check the **Related** questions right of the this answer ;). – Tarek Hallak Nov 27 '13 at 08:25
5 Answers
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