Reputation: 51
Im making my first iPhone app, and trying to access one class function from another class function. Why is this not working?
Basically I have two classes, "DateViewController" and "inputMilesViewController". In "DateViewController" I took a date from a date pointer and turned it into a string, which I want to send to a function in "inputMilesViewController" which will change a label on a button within that scene to display the string which is passed in.
In DateViewController.m I have entered #import "inputMilesViewController.h" I tried doing
[inputMilesViewController changeButtonText:msg];
*I cant get passed error message "No known class method for selector 'changeButtonText:'
inputMilesViewController.m has the following ...
-(void) changeButtonText:(NSString*) dateStringForInput{
NSLog(@"we got to changebuttontext");
//[_myTodayButton setTitle:dateStringForInput forState:UIControlStateNormal];
}
for now I am less concerned with actually changing the text and am just trying to even get it to print this NSLog just so I know I can get into the function.
Upvotes: 0
Views: 154
Reputation: 69027
[inputMilesViewController changeButtonText:msg];
*I cant get passed error message "No known class method for selector 'changeButtonText:'
You are sending the message to the class instead of to an instance of that class. That explains the issue.
The way to solve this is making one controller know about the other, so that you can send the message to the known instance of the controller. This can be done by a technique which is known as "dependency injection", which in simple words means that you "inject" a reference to your inputMilesViewController
instance into the other controller, so that the latter can send a message to the former.
You can, e.g., pass a pointer to your inputMilesViewController
instance into DateViewController
's initialization method.
@interface DateViewController
...
@property (nonatomic, retain/strong) inputMilesViewController* inputMilesController;
...
- (id)initWithInputMilesViewController:(inputMilesViewController*)controller;
...
@end
@implementation DateViewController
...
[self.inputMilesController changeButtonText:msg];
...
@end
Or you can simply define a property in DateViewController
and set its value at the right time (i.e, before calling dateStringForInput
).
A good way of dealing with dependency injection in Objective-C is through the concept of "delegate" or "dataSource". (I hope all this sounds familiar to you, so you can find your way to a good design).
Another approach would be using the notification center, so instead of sending a message directly to inputMilesViewController
, you send it through the notification center. This solution ensures a lower coupling between the two classes.
In this post you can find a description of how to implement it.
EDIT:
Calling the class method (+
instead of -
) cannot be the solution, since you want to really access the class instance, not the class itself. A class method is good, when you have, e.g., static data that all instances of the class share. In this case you have a controller which is instantiated to back up some view on screen; that view has some button in it and you want to change its text. This is all inherently defined at the instance level.
Have a look at this post for clarifications about the difference between class and instance methods.
My suggestion is for you to work out one of the methods I described above to properly address communication between the two controllers. It is such a general issue that you will have to address it over and over across your programming projects. If you pick up one and would like to have more detail, just ask for it.
Upvotes: 2