Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 7如何获取当前选定的UITextField id_Ios_Iphone - Fatal编程技术网

iOS 7如何获取当前选定的UITextField id

iOS 7如何获取当前选定的UITextField id,ios,iphone,Ios,Iphone,在调用dismissKeyboard时,我将每个UITextField添加到resignFirstResponder 是否有办法获取当前选定UITextField的id并使其辞职FirstResponder #import "NewBabyViewController.h" @interface NewBabyViewController () @property (weak, nonatomic) IBOutlet UITextField *BabyName; @property (weak

在调用dismissKeyboard时,我将每个UITextField添加到resignFirstResponder

是否有办法获取当前选定UITextField的id并使其辞职FirstResponder

#import "NewBabyViewController.h"

@interface NewBabyViewController ()
@property (weak, nonatomic) IBOutlet UITextField *BabyName;
@property (weak, nonatomic) IBOutlet UITextField *BabyNickname;    
@end

@implementation NewBabyViewController

- (void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)
                                   ];
    [self.view addGestureRecognizer:tap];
}

-(void)dismissKeyboard{

    [self.view endEditing:YES];
    //[self.BabyName resignFirstResponder];
    //[self.BabyNickname resignFirstResponder];
}

-(IBAction)textFieldDone:(id)sender
{
    [sender resignFirstResponder];
}

@end

这个问题的解决方案并不十分明显,但实际上非常容易解决。只需使用以下命令:

-(void)dismissKeyboard {
    [self.view endEditing:YES];
}

你应该自己追踪第一反应者。有多种方法可以做到这一点,但最简单的方法是更新
textfielddebeginediting:

如果您希望进入灰色区域,则在
UIApplication
UIWindow
-(id)firstResponder
上都有一个私有API方法,它将为您提供当前的firstResponder。我和他们有很好的合作记录。我更喜欢
UIWindow
方法,它在涉及多个窗口时更安全

另一种方式:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

这实际上很酷,但我认为它不会从
contentEditable
web视图(或作为第一响应者的内部子视图)中删除第一响应者:(这真是太好了。在此之前,我会浏览所有子视图,找到作为第一响应者的UITextField,这样我就可以告诉它辞职了。:@0x7fffffff非常感谢。这正是我想要找到的。我根据你的答案编辑了上面的示例代码。我喜欢sendAction:to:nil技巧。使用响应器链将辞职消息传递给链中的第一个对象。聪明。(表决)