开发者

iPhone - Unrecognized Selector, "may not respond to addTarget"

I'm working on a basic iPhone app to test some events, and I'm running into an error that I can't understand or find any answers for. I'm not using IB at all (aside from the MainWindow.xib it creates).

Right now it's about as basic as it could be.

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

@s开发者_StackOverflowynthesize 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

mainViewController.h

#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController {

}

- (void)showMenu;

@end

mainViewController.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

Now, I get a warning on this line:

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

that says 'UIView may not respond to '-addTarget:action:forControlEvents:'. And this doesn't make sense, because a UIView subclass can certainly respond to addTarget, and I'm calling it on self.view, which must exist because I don't release it until the end of loadView. (and even then it should be retained by the controller)

Looking at a trace shows that the actual error is -[UIScrollView addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x5f11490

So it looks like it's a problem with the selector itself, but I see nothing wrong with my selector!

I'm pretty baffled by this and any help would be great.


First, classes always start with capital letters....

UIScrollView is a subclass of UIView, not UIControl.

UIControl implements addTarget:action:forControlEvents:. UIScrollView does not. Thus, the runtime error.

If you want something to happen in response to an action taken on the scroll view, set up a delegate for the scroll view. See UIScrollViewDelegate's documentation.


try this subtle change

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

and this

- (void)showMenu:(id) sender {
    NSLog(@"Show Menu");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜