使用手势识别器时出现“无法识别的选择器错误”

时间:2015-08-04 19:56:58

标签: ios objective-c uitapgesturerecognizer

点击视图时收到以下错误:

2015-08-04 15:42:16.236 jake-and-bailey[12158:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainMenuViewController openSettingsPage]: unrecognized selector sent to instance 0x7af560f0'

我在该视图中添加了以下手势识别器:

接口

#import <UIKit/UIKit.h>

@interface OpenSettingsGestureRecognizer : UITapGestureRecognizer

@property UIViewController* viewController;

- (instancetype) initWithViewController: (UIViewController*) viewController;

@end

实施

#import "OpenSettingsGestureRecognizer.h"
#import "SettingsViewController.h"

@implementation OpenSettingsGestureRecognizer

- (instancetype) initWithViewController: (UIViewController*) viewController {
    self = [super initWithTarget:viewController
                          action:@selector(openSettingsPage)];
    if (self) {
        _viewController = viewController;
    }
    return self;
}

- (void) openSettingsPage {
    if (_viewController != nil) {
        [_viewController presentViewController:[[SettingsViewController alloc] init]
                                      animated:YES
                                    completion:nil];
    }
}

@end

无论出于何种原因,从我的 MainMenuViewController 类而不是 OpenSettingsGestureRecognizer 类调用 openSettingsPage 方法。

2 个答案:

答案 0 :(得分:1)

将目标更改为self,您可以这样做:

 self = [super initWithTarget:self
                          action:@selector(openSettingsPage)];

了解目标行动设计模式here

答案 1 :(得分:1)

您正在使用UITapGestureRecognizer初始化其UIViewController子类的OpenSettingsGestureRecognizer初始化self = [super initWithTarget:viewController action:@selector(openSettingsPage)]; 目标。尝试更改:

self = [super initWithTarget:self
                      action:@selector(openSettingsPage)];

OpenSettingsGestureRecognizer

如果您希望目标为MainMenuViewController而不是UIViewController(我假设您使用curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d doc 来初始化手势。