我有一个主板(UIViewController),它实例化卡对象(UIViewControllers)。在每张卡上都有一个 texfield 。要通过单击非卡区域(=电路板视图)来移除键盘,我需要参考 UITapGestureRecognizer 中指定的电路板。这是我目前的做法。
Board (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方法的“background”引用似乎不起作用。为什么呢?
我如何从卡片引用回到电路板上以便随时重新启动卡片的第一响应者?
答案 0 :(得分:3)
尝试将手势代码添加到Board而不是Card(在viewDidLoad中)
UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackground:)];
[self.view addGestureRecognizer:tapBackground];
答案 1 :(得分:1)
在ViewController.h中
@interface ViewController : UIViewController {
IBOutlet UITextField *textField
}
-(IBAction)bgTouched:(id)sender;
@end
在ViewController.m中
-(IBAction)bgTouched:(id)sender {
[textField resignFirstResponder]
}
4。将textField连接到Text Field并将bgTouched连接到后台的按钮。