我有一个(UIViewController),它实例化对象(UIViewControllers)。在每张卡上都有一个 texfield 。为了通过单击非卡区域(=面板视图)来移除键盘,我需要引用 UITapGestureRecognizer 中指定的面板。这是我目前的方法。

(UIViewController)初始化卡对象

-(void) addCard:(id)touchEvent{
    CardViewController *card = [[CardViewController alloc]initItemWithText:@"..."];
    [self addChildViewController:card];
    [self.view addSubview:card.view];
}

(UIViewController)在初始化时,添加Tap Gesture Recognizer
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
...
    UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackground:)];
    [self.parentViewController.view addGestureRecognizer:tapBackground];
...
}

使用parentViewController方法的“背景”引用似乎无效。为什么?

我如何才能从卡上参考到板子,以便在点击时辞去卡的第一响应者?

最佳答案

尝试将手势代码添加到Board而不是Card(在viewDidLoad中)

UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackground:)];
[self.view addGestureRecognizer:tapBackground];

10-08 20:05