0

Hi I am trying to call a method from another class and it is not working.

Here is the method I want to call, "updateName".

@interface Content1ViewController ()

@end

@implementation Content1ViewController

+ (void)updateName:(id)sender {
    NSLog(@"IT WORKED");
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

@end

Then I try call it in a method from another class.

-(void)randomMethod {

[Content1ViewController updateName];

}

Then I get a error at "update name" as it doesn't recognise the method? I followed this answer and its not working for me. I have made sure I have imported the classes to each other aswell.

Can someone please tell me why its not working? Thanks.

Community
  • 1
  • 1

4 Answers4

3

Make sure you declare the method in header file of Content1ViewController, that will make it public. Also, the signature of the caller method is different as of Originally declared method. The updateName is expecting a parameter of (id)sender, Make sure you pass it some reference to call the right method.

Jamal Zafar
  • 2,179
  • 2
  • 27
  • 32
0

Here updateName is a method with sender as a parameter.

[Content1ViewController updateName:SOME_SENDER_ID_INSTANCE]; //Should work. Because your method definition says it needs `id` as param.
Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61
Ayyappa
  • 1,876
  • 1
  • 21
  • 41
0

updateName has got a parameter sender and you are not passing it while calling the method and hence you are getting error

user3365619
  • 95
  • 1
  • 1
  • 8
0

Your declaration doesn't match the method signature i.e. you need to call:

[Content1ViewController updateName:SOMETHING_HERE]; 
CW0007007
  • 5,681
  • 4
  • 26
  • 31