Dismiss The Keyboard - Multiple UITextFields in iOS 7

前端 未结 8 1149
盖世英雄少女心
盖世英雄少女心 2021-01-31 22:56

below you\'ll find my .h & .m files for my primary viewcontroller.

I have 3 questions.

1.) Because I have multiple uitextfields, do I have to set each with

8条回答
  •  失恋的感觉
    2021-01-31 23:17

    Here's what I use in my code. It works great and is more efficient than the other answers.

    In yourviewcontroller.h add:

    @property (nonatomic) UITapGestureRecognizer *tapRecognizer;

    Now in the .m file, add this to your ViewDidLoad function:

    - (void)viewDidLoad {
        //Keyboard stuff
        tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
        tapRecognizer.cancelsTouchesInView = NO;
        [self.view addGestureRecognizer:tapRecognizer];
    }
    

    Also, add this function in the .m file:

    - (void)handleSingleTap:(UITapGestureRecognizer *) sender
    {
        [self.view endEditing:YES];
    }
    

提交回复
热议问题