触摸屏幕时,IOS UIScrollview不允许键盘关闭

时间:2015-03-03 04:02:01

标签: ios objective-c uiscrollview

我构建了一个应用程序,当填写文本视图时,键盘会弹出并允许您键入。如果我决定我不想要键盘,我可以点击背景视图控制器,它就会消失。现在,我添加了一个scrollview。一切正常,但是当我在textview外面触摸时,我无法进行连接并使键盘掉落。 (我无法在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];
}

1 个答案:

答案 0 :(得分:0)

尝试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];
}