Hiding the Keyboard when losing focus on a UITextView

后端 未结 10 536
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 10:34

So I have a UITextView that I\'m using to allow the user to submit some text.

My problem is, I can\'t seem to figure out how to allow the user to \'Cancel\' by tappi

相关标签:
10条回答
  • 2020-12-04 10:35

    In my view:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
    
        // pass touches up to viewController
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    

    In my viewcontroller:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [[event allTouches] anyObject];
        if (keyboardShown && [touch view] != self.TextView) 
        {
             [self.TextView resignFirstResponder];
        }
        [super touchesBegan:touches withEvent:event];
    }
    
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {    
            keyboardShown = YES;
    }
    
    0 讨论(0)
  • 2020-12-04 10:37

    Another approach I used a lot when working with UITableViews, Searchbar and keyboard is this, I think you can apply it to text field

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.tableView addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.cancelsTouchesInView = NO;  // this prevents the gesture recognizers to 'block' touches
    [gestureRecognizer release];
    

    And then, here's the simple callback function

    - (void)hideKeyboard {
        [myTextField resignFirstResponder];
    }
    

    Hope it helps

    0 讨论(0)
  • 2020-12-04 10:41

    Simplifying tuzzolotron's answer: Where the following outlet is properly connected in your xib

        IBOutlet UITextView *myTextView;
    

    Use this in the view controller:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [[event allTouches] anyObject];
        if ([myTextView isFirstResponder] && [touch view] != myTextView) {
            [myTextView resignFirstResponder];
        }
        [super touchesBegan:touches withEvent:event];
    }
    

    A tap on the View Controller's View, outside of the Text View, will resign the first responder, closing the keyboard.

    0 讨论(0)
  • 2020-12-04 10:43

    Very easy

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.view endEditing:YES];
    }
    
    0 讨论(0)
  • 2020-12-04 10:43

    I wanted a version of this that worked for resigning from any view, without requiring specific outlets for each one.. Here is my solution, which I put into one of my standard container views..

    It's using MonoTouch C#, though it should be obvious how to convert it to Objective-C

        private void findAndResignFirstResponder(UIView node=null) {
            if (node == null) { return; }
            if (node.IsFirstResponder) {
                node.ResignFirstResponder();
                return;
            }
    
            foreach (UIView view in node.Subviews) {
                findAndResignFirstResponder(node:view);
            }
        }
    
        private bool haveDrag = false;
        public override void TouchesEnded(NSSet touches, UIEvent evt) {
            if (!haveDrag) {
                UITouch touch = (UITouch)evt.AllTouches.AnyObject;
                if (!touch.View.CanBecomeFirstResponder) {
                    this.findAndResignFirstResponder(this);
                    Console.WriteLine("resign");
                }
            }
            base.TouchesEnded (touches, evt);
        }
        public override void TouchesMoved(NSSet touches, UIEvent evt) {
            haveDrag = true;
            base.TouchesMoved (touches, evt);
        }
    
        public override void TouchesBegan(NSSet touches, UIEvent evt) {
            haveDrag = false;
            base.TouchesBegan (touches, evt);
        }
    
    0 讨论(0)
  • 2020-12-04 10:47
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        var touch = event.allTouches()?.anyObject() as UITouch
    
        if( textfield.isFirstResponder() && touch.view != textfield) {
            textfield.resignFirstResponder()
            NSLog("Resigning first responder since touches began outside")
        }
    
        super.touchesBegan(touches, withEvent: event)
    }
    

    ohhorob's answer worked for me too. Here's the simple swift equivalent.

    0 讨论(0)
提交回复
热议问题