calling function problem (iphone)
From class picViewController i am calling function imageCliked on scrollViewController in order to fire function loadPage but it does work. Compiler error:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[scrollViewController loadpage:]: unrecognized selector sent to class 0x17198'"
Anybody can help?
@interface scrollViewController : UIViewController <UIScrollViewDelegate> {
}
- (void)loadPage:(int)page; // loads a new picViewController
+(void) imageCliked;
@end
#import "scrollViewController.h"
#import "picViewController.h"
#import "MLUtils.h"
@implementation scrollViewController
- (void)loadPage:(int)page {
// I need to call this function from +(void) imageCliked
}
/* this function is called by picViewController pressButton1 */
+(void) imageCliked {
NSLog(@"left");
[self loadPage:3];// does 开发者_开发技巧not work
}
@end
#import "picViewController.h"
#import "scrollViewController.h"
@implementation picViewController
- (void) pressButton1:(id)sender{
[scrollViewController imageCliked];
}
@end
If it is a class, please name it starting with a capital letter.
[self loadPage:3];
Since self
is a class in +imageCliked
, the loadPage:
method must be a class method as well. But you're declaring -loadPage:
as an instance method. The two are not exchangeable. Either
- Make
+loadPage:
a class method (change the-
to+
), or - Create a temporary instance of
scrollViewController
, i.e.[[[[self alloc] init] autorelease] loadPage:3];
, or - Make
-imageCliked
an instance method, and create an instance in-pressButton1:
.
You have a mismatch between class methods and instance methods. Methods that start with +
belong to the class, and ones that start with -
belong to the instance.
The specific error you see above happens because you're trying to call an instance method (-loadPage
) from a class method (+imageClicked
), which doesn't work-- "self" inside of imageClicked refers to the whole class, so that's why it fails.
My guess is that you actually want both of those methods to be instance methods. Make them both prefixed with -
.
But it sounds like you might need to review some basics in the first couple chapters of Apple's Objective-C documentation.
精彩评论