how can I dismiss keyboard when I will just touch anywhere except UITextField?

后端 未结 10 2002
独厮守ぢ
独厮守ぢ 2021-02-19 11:12

Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?

相关标签:
10条回答
  • 2021-02-19 11:32

    Keep a reference to the UITextField in your view controller and call [yourTextField resignFirstResponder]

    0 讨论(0)
  • 2021-02-19 11:36

    The answer by Ishu is a good one. But I think you should also give a try to :

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
      [self.view endEditing:YES];
    }
    
    0 讨论(0)
  • 2021-02-19 11:43

    Use delegate,

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
     [textField resignFirstResponder];
     return YES;
    }
    

    You can also create a method in your controller

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

    and then in Connection Inspector in IB connect Event "Did End On Exit" to it.

    0 讨论(0)
  • 2021-02-19 11:44

    Create an invisible button, put it behind the UITextField and send resignFirstResponder message to your text fields when the button is tapped.

    0 讨论(0)
  • 2021-02-19 11:45

    Typically in this scenario you will have a scroll view holding the rest of the User Interface. If you assign the delegate of that scroll view to your view controller, and say that you implement the protocol UIScrollViewDelegate, then just adding the following method will do the job:

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        [self.view endEditing:YES];
    }
    
    0 讨论(0)
  • 2021-02-19 11:45
    You need to define the action for your button click this way
    
    [infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    
    And implement the click method ,as for example i have provided the animation on button click method.
    -(void)backButtonClicked
    {
    
        [UIView beginAnimations:@"animation" context:nil];
        [UIView setAnimationDuration:1.5];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
        [self.navigationController popViewControllerAnimated:YES]; 
        [UIView commitAnimations];
    
    }
    
    
    Hope this will help you ,the action is without using IB.Its all done through coding
    
    0 讨论(0)
提交回复
热议问题