0

我正在创建一个可以继承的自定义 UITableViewController (SATableViewController) 类,它将提醒我何时应该从文本字段等中退出第一响应者。

但是,在加载视图(viewDidLoad,[self.tableView addGestureRecognizer:_tapGestureRecognizer];下面代码中指示的行)时,会引发以下异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

手势识别器被分配并初始化得很好,所以我很难理解为什么会发生这种情况。这个手势识别器的东西都是 RootTableViewController 的一部分,它工作得很好。

这就是我所拥有的(相关部分):

SATableViewController.h:

#import <UIKit/UIKit.h>

@interface SATableViewController : UITableViewController <UIGestureRecognizerDelegate>

@end

SATableViewController.m:

@interface SATableViewController ()

@property (nonatomic, strong) UIGestureRecognizer *tapGestureRecognizer;

@end

@implementation SATableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add gesture to detect when table view is being tapped so that keyboard may be dismissed
    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(dismissKeyboard)];
    _tapGestureRecognizer.delegate = self;
    _tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:_tapGestureRecognizer]; // EXCEPTION THROWN HERE
}

//...

//see: http://stackoverflow.com/questions/7195661/why-is-uigesturerecognizer-being-called-on-my-textfield-clear-button
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isKindOfClass:[UITextField class]] ||
       [touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

- (void)dismissKeyboard {
    // to be overidden by subclass -- called when text field should be dismissed
}

//...

- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView {
    [self dismissKeyboard];
}

RootTableViewController.h:

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

@interface RootTableViewController : SATableViewController

@end

RootTableViewController.m:

@interface RootTableViewController ()

@property (nonatomic, strong) UIGestureRecognizer *tapGestureRecognizer;

@end

@implementation RootTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

- (void)dismissKeyboard {
    if ([_textField isFirstResponder]) {
        [_textField resignFirstResponder];
    }
}
//...
4

1 回答 1

0

刚刚想通了。这是因为我没有摆脱@property (nonatomic, strong) UIGestureRecognizer *tapGestureRecognizer;RootTableViewController.m 。

所以

@interface RootTableViewController ()

@property (nonatomic, strong) UIGestureRecognizer *tapGestureRecognizer;

@end

本来应该

@interface RootTableViewController ()

@end
于 2013-05-28T03:59:50.963 回答