UITextView resign first responder on 'Done'

前端 未结 2 1771
鱼传尺愫
鱼传尺愫 2021-02-05 17:45

I have been searching around the web for about an hour now, and cannot find any code to help me with this.

I have a UITextView that I need to resign first r

2条回答
  •  难免孤独
    2021-02-05 18:03

    Implement the shouldChangeTextInRange: delegate method.

    Use below approach and the solution work only with @"\n" (new line character).

    //In you *.h file make sure you add
    @interface v1ViewController : UIViewController 
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        myTextField.delegate = self;
    }
    
        - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
              replacementText:(NSString *)text
            {
    
                if ([text isEqualToString:@"\n"]) {
    
                    [textView resignFirstResponder];
                    // Return FALSE so that the final '\n' character doesn't get added
                    return NO;
                }
                // For any other character return TRUE so that the text gets added to the view
                return YES;
        }
    

提交回复
热议问题