0

我正在开发一个基本的 iPhone 应用程序来测试一些事件,我遇到了一个我无法理解或找不到任何答案的错误。我根本没有使用 IB(除了它创建的 MainWindow.xib)。

现在它已经尽可能基本了。

mainAppDelegate.h

#import <UIKit/UIKit.h>
#import "mainViewController.h"

@interface mainAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    mainViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) mainViewController *viewController;

@end

mainAppDelegate.m

#import "mainAppDelegate.h"

@implementation mainAppDelegate

@synthesize window;
@synthesize viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    self.viewController = [[mainViewController alloc] init];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

@end

主视图控制器.h

#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController {

}

- (void)showMenu;

@end

主视图控制器.m

#import "mainViewController.h"

@implementation mainViewController

- (void)loadView {
    UIScrollView *mainView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    mainView.scrollEnabled = YES;
    self.view = mainView;    
    self.view.backgroundColor = [UIColor grayColor];

    [self.view setUserInteractionEnabled:YES];
    [self.view addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchDown];

    [mainView release];
}

- (void)showMenu {
    NSLog(@"Show Menu");
}

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

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

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

@end

现在,我在这一行收到警告:

[self.view addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchDown];

说'UIView 可能不响应'-addTarget:action:forControlEvents:'。而且这没有意义,因为一个UIView子类肯定可以响应addTarget,而我在self.view上调用它,它必须存在,因为我直到loadView结束才释放它。(即使那样它也应该由控制器保留)

查看跟踪显示实际错误是 -[UIScrollView addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x5f11490

所以看起来这是选择器本身的问题,但我发现我的选择器没有任何问题!

我对此感到非常困惑,任何帮助都会很棒。

4

2 回答 2

3

首先,类总是以大写字母开头......

UIScrollView是 的子类UIView,不是UIControl

UIControl实现addTarget:action:forControlEvents:UIScrollView才不是。因此,运行时错误。

如果您希望响应在滚动视图上执行的操作而发生某些事情,请为滚动视图设置委托。请参阅UIScrollViewDelegate的文档

于 2010-07-22T16:04:21.457 回答
0

试试这个微妙的变化

[self.view addTarget:self action:@selector(showMenu:)
                               forControlEvents:UIControlEventTouchDown];

和这个

- (void)showMenu:(id) sender {
    NSLog(@"Show Menu");
}
于 2010-07-22T15:59:27.287 回答