0

I recently created a method that can be called from other classes, this is my code.

In the ViewController1.h

+ (void)updateName:(id)sender;

In the ViewController1.m

+ (void)updateName:(id)sender {

}

The method is calling and working which is good however I have another method in the ViewController1.m file which is

-(void)updateString {
        NSLog(@"IT WORKED");
}

However, I try to call it in my updateName method like this:

+ (void)updateName:(id)sender {
    [self updateString];
}

But I get an error saying "no known class method for selector 'updateString' " Can anyone tell why is this happening and how I can call this method? Thanks.

  • 3
    You can't call an instance method from a class method. – rmaddy May 20 '15 at 06:58
  • building on top of rmaddy's comment, + means class method, - means instance method, incase you dont know – Fonix May 20 '15 at 06:59
  • @rmaddy +1 But of course you can send a message to an instance object inside a class object's method. But you should not use `self`. ;-) – Amin Negm-Awad May 20 '15 at 07:01
  • Is there anyway to call an instance method from a class method, or a way around it? – Cool Kat Studios May 20 '15 at 07:01
  • no, but maybe you dont need it to be a instance method, change the - to a + the `[self updateString];` to `[ViewController1 updateString];`and see if your problems are solved, but you may run into issues later if you arent understanding why this is a problem – Fonix May 20 '15 at 07:05
  • Thanks, but this is not ideal as I am trying to set a string to a different value in the class method. Any ways to do that? – Cool Kat Studios May 20 '15 at 07:08
  • 1
    then everything must be made to be instance methods, and pass the instance of `ViewController1` to where ever you are trying to access it, im guessing you are trying to set the text of a viewcontroller you are about to segue to? – Fonix May 20 '15 at 07:11
  • @Fonix Yes that is exactly what I am trying to do! How can I pass an instance method to be called from another view controller? – Cool Kat Studios May 20 '15 at 07:16
  • [this should help you](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Fonix May 20 '15 at 07:19
  • @Fonix I have tried your suggestion and I think I am halfway there! Content1ViewController *viewController1 = [Content1ViewController alloc]; viewController1.nameLabel.text = @"HELLO"; That is my code, however "nameLabel" is a UILabel and it doesn't seem to be being set? Any reason for this? Thanks. – Cool Kat Studios May 20 '15 at 07:36
  • first of all, use `[Content1ViewController new];` you cant alloc without initing if you do it that way, i think what you need to do is look at the 2nd method that was suggested in that other thread where it uses the `prepareForSegue`. but also on top of that, dont set the label directly, use a separate `@property` you can set, then in the `viewDidLoad` set the label with the text, the label only exists when the viewDidLoad fires (if it is being made by a storyboard) – Fonix May 20 '15 at 07:42
  • @Fonix right, the only problem is I want to regularly call to update the string, thats why I cant use the view did load because I am actually using a page view controller and if i am correct I believe the viewDidLoad method is only called once? – Cool Kat Studios May 20 '15 at 07:50
  • you may want to use an entirely different approach then, im not sure what you are trying to do, maybe you can use `NSUserDefaults`, maybe the viewcontrollers in the pager should be delegates to the parent viewcontroller. dont really have time to explain how each method would work, but maybe hunt around this site for other answers that could explain how they work, i would probably recommend a delegate approach – Fonix May 20 '15 at 08:13

2 Answers2

0

The + in front of the method denotes a class method i.e. you don't need to create a new instance of ViewController1 in order to call the method. Where as [self updateString] is an instance method i.e. you need to create a new instance of the class in order to perform it. The problem here is the difference in scope. See below:

Inside another class:

[ViewController1 updateName:SOMETHING];

vs

ViewController1 *newViewController1 = [[ViewController1 alloc] init];
[newViewController1 updateString];

EDIT

If you want to call that method from that class you can do this?

+ (void)updateName:(id)sender {
    [ViewController1 updateString];
}

But that won't be able to reference or update any class properties etc.. So from the names of your methods, this is likely not going to solve your problem.

Does that make sense ?

CW0007007
  • 5,681
  • 4
  • 26
  • 31
  • Ok, so I think I understand you can't call an instance method from a class method, but now is there anyway to get around it and some how call an instance method from a class method? Thanks. – Cool Kat Studios May 20 '15 at 07:03
  • What are you trying to do ? This sounds like na xy problem. – CW0007007 May 20 '15 at 07:04
  • I am trying to update a view controllers string from a different view controller, hence why I need to be able to reference strings and values from a class method. Any help or solutions towards this challenge? Thanks. – Cool Kat Studios May 20 '15 at 07:06
0

You're doing something odd but to fix your problem simply replace the - with + in front of your method name.

Gil Sand
  • 5,802
  • 5
  • 36
  • 78