【问题标题】:iPhone - Unrecognized Selector, "may not respond to addTarget"iPhone - 无法识别的选择器,“可能无法响应 addTarget”
【发布时间】:2010-07-22 15:49:00
【问题描述】:

我正在开发一个基本的 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

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

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

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

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

查看跟踪显示实际错误是 -[UIScrollView addTarget:action:forControlEvents:]: 无法识别的选择器发送到实例 0x5f11490

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

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

【问题讨论】:

    标签: iphone uiscrollview uicontrol unrecognized-selector


    【解决方案1】:

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

    UIScrollViewUIView 的子类,而不是UIControl

    UIControl 实现addTarget:action:forControlEvents:UIScrollView 没有。因此,运行时错误。

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

    【讨论】:

    • +1 表示UIControl。作为进一步的了解,UIControl 是为您提供所有这些UIControlEvents 的原因,例如 touch down 或 touch up。如果对象不是UIControl 的子类,则不能在其上使用这些事件。一个简单的示例是打开一个包含视图的 XIB。单击视图,进入检查器窗口,您会看到它没有这些控制事件。现在,如果您转到检查器窗口的最后一个选项卡并将其设置为 UIControl 子类而不是 UIView 它神奇地拥有所有您可以连接的事件。
    • 谢谢,这正是我需要知道的。我应该知道检查一下。当你说“课程总是以大写字母开头”时——这是惯例还是必要的?
    • Objective-C 约定,尽管我们中的许多人认为有必要避免导致混乱的精神错乱。 :)
    【解决方案2】:

    试试这个微妙的变化

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

    还有这个

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

    【讨论】:

    • 谢谢亚伦,解决了我的问题。我正在关注官方 facebook SDK 文档,但冒号丢失了。
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多