呼叫功能问题(iphone)

时间:2010-03-07 15:10:59

标签: iphone objective-c

从类picViewController我在scrollViewController上调用函数imageCliked 为了激活函数loadPage但它确实有效。编译器错误:

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[scrollViewController loadpage:]: unrecognized selector sent to class 0x17198'"

有人可以帮忙吗?

@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

2 个答案:

答案 0 :(得分:4)

如果是班级,请以大写字母开头命名。


[self loadPage:3];

由于self+imageCliked中的一个类,因此loadPage:方法也必须是类方法。但您将-loadPage:声明为实例方法。这两个是不可交换的。任

  1. 使+loadPage:成为一种类方法(将-更改为+)或
  2. 创建scrollViewController的临时实例,即[[[[self alloc] init] autorelease] loadPage:3];
  3. 使-imageCliked成为实例方法,并在-pressButton1:
  4. 中创建实例

答案 1 :(得分:1)

类方法和实例方法之间存在不匹配。以+开头的方法属于该类,而以-开头的方法属于该实例。

您在上面看到的具体错误是因为您尝试从类方法(-loadPage)调用实例方法(+imageClicked),这种方法不起作用 - 内部“自我” imageClicked指的是整个类,这就是它失败的原因。

我的猜测是你真的希望这两种方法都是实例方法。使它们都以-为前缀。

但听起来你可能需要在Apple's Objective-C documentation的前几章中回顾一些基础知识。