How to hide Textbox Keyboard when “Done / Return” Button is pressed Xcode 4.2

前端 未结 3 1184
孤街浪徒
孤街浪徒 2021-01-30 12:52

I created a Text Field in Interface Builder. I set it\'s \"Return Key\" to Done. This is a one line only input (so it wont need multiple lines).

How do I hide the virt

相关标签:
3条回答
  • 2021-01-30 13:19

    UITextView does not have any methods which will be called when the user hits the return key.

    Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.

    There might be other ways but I am not aware of any.

    Make sure you declare support for the UITextViewDelegate protocol.

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
    replacementText:(NSString *)text {
    
        if([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            return NO;
        }
        return YES;
    }
    

    I hope it will helps a lot.

    0 讨论(0)
  • 2021-01-30 13:19

    Make category on UIViewController with next method

    - (void)hideKeyboard
    {
        [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
                                                   to:nil
                                                 from:nil
                                             forEvent:nil];
    }
    
    0 讨论(0)
  • 2021-01-30 13:23

    Implement the delegate method UITextFieldDelegate, then:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.yourIBtextField.delegate = self;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        return NO;
    }
    
    0 讨论(0)
提交回复
热议问题