开发者

UIKeyboard return button action different from UITextView resignFirstResponder

I have a UITextView for the user to enter som text. When the UITextView is called with:

[textView becomeFirstResponder];

I add a rightBarButtonItem, which lets the user to dismiss the UIKeyboard with:

[textView resignFirstResponder];

I wonder if I can wire the return key of the UIKeyboard to do some action but also to dismiss it self. Action is to save some data added to the textView.

So how can implement to actions, where both call resignFirstResponder but do different things.

Here is the code i use.

-(void)viewDidLoad {
  self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
    initWithTitle:@"Cancel"
    style:UIBarButtonItemStyleBordered
    target:self action:@selector(cancelEditing)] autorelease];
}
- (void)textViewDidEndEditing:(UITextView *)textview {
    [self saveSomeData];
}

- (void)cancelEditing {
    [commentTextView resignFirstResponder]; 
}

I this possible do I understand that resignFirstResponder also calls textViewDidEndEditing in both cases.

Thanks in 开发者_运维百科advance!


For a UITextView you need to implement the UITextViewDelegate and use the method:

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

Check the text parameter, if it is equal to "\n", then you can dismiss the keyboard.

EDIT: Was talking about TextView, keeping below just in case it's needed.

Set your controller to be the delegate of the UITextField. Then implement:

- (BOOL)textFieldShouldReturn:(UITextField *)textField

In this method you can resign first responder.

The UITextField should also Auto-enabled the Return key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜