IOS UIS滚动视图不允许在触摸屏幕时关闭键盘



我构建了一个应用程序,当填充文本视图时,键盘会弹出并允许您键入。如果我决定不想让键盘向上,我可以点击背景视图控制器,它就会掉下来。现在,我添加了一个滚动视图。一切都很好,但当我在文本视图外触摸时,我无法进行连接并使键盘脱落。(我无法在Xcode中制作自定义视图控制器)

有人有什么修复方法吗?

提前谢谢!

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *actionField;
@property (weak, nonatomic) IBOutlet UITextField *impactField;
@property (weak, nonatomic) IBOutlet UITextField *resultField;
@end
@implementation ViewController
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(

    NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory    stringByAppendingPathComponent:@"data.plist"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager]
         fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc]
                          initWithContentsOfFile:filePath];
        for (int i = 0; i < 3; i++) {
            UITextField *theField = self.lineFields [i];
            theField.text = array [i];
        }

 }
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                               selector:@selector(applicationWillResignActive:)
                                                name:UIApplicationWillResignActiveNotification
                                              object:app];
}
    - (void)applicationWillResignActive:
    (NSNotification *)notification
    {
    NSString *filePath = [self
                          dataFilePath];
    NSArray *array = [self.lineFields valueForKey:@"text"];
    [array writeToFile:filePath atomically:YES];
 }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
- (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)backgroundTap: (id)sender {
    [self.actionField resignFirstResponder];
    [self.impactField resignFirstResponder];
    [self.resultField resignFirstResponder];
}

尝试UITapGestureRecognizer:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScrollView:)];
[scrollView addGestureRecognizer:singleTap];

行动:

- (void)tapScrollView:(UITapGestureRecognizer *)gesture { 
    [self.actionField resignFirstResponder];
    [self.impactField resignFirstResponder];
    [self.resultField resignFirstResponder];
}

最新更新