UIText view delegate issue

怎甘沉沦 提交于 2019-12-13 15:26:25

问题


I want to resign keyboard from UItextview. How to implement UItextView delegate method programmatically.


回答1:


If u want that ur keyboard is resigned when click on return then u have to write, implement this method....

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

if([text isEqualToString:@"\n"]) {
    [textView resignFirstResponder];
    return NO;
}

return YES;

}

Just make it copy and paste....:)




回答2:


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;
}



回答3:


You should use the UITextViewDelegate. You have to declare the use of the protocol in your class header like:

@interface YourClass:NSObject<UITextViewDelegate>

Then in your .m, you should set your class as delegate in some point with something like:

textView.delegate = self;

Then, in your .m again, you have to implement the delegate methods, in particular:

textViewDidChange:

You can read the protocol reference at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html for more information.




回答4:


There is no specific delegate method for UITextview to know when user hits "RETURN" So you can do like this

//In .h File

@interface BlahBlah : UIViewController <UITextViewDelegate>
  @property(nonatomic, retain) IBOutlet UITextView *myTextView;
@end

//In .m File
@implementation BlahBlah

@synthesis myTextView;

//In some method, can be viewDidLoad OR viewDidAppear . your convenience ;) 
{
   self.myTextView.delegate = self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

        if([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            return NO;
        }

        return YES;
}


来源:https://stackoverflow.com/questions/9737017/uitext-view-delegate-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!