如何更改Xcode键盘上“返回”按钮的操作?

时间:2015-05-04 12:59:33

标签: ios text keyboard return action

我需要更改键盘上“return”按钮的文本,我甚至需要更改其动作。 该行动应该是一个新的段落。 你能救我吗?

5 个答案:

答案 0 :(得分:2)

您无法将“返回”按钮重命名为任何自定义文字

返回键可以采用的一些默认值是

typedef enum : NSInteger {
   UIReturnKeyDefault,
   UIReturnKeyGo,
   UIReturnKeyGoogle,
   UIReturnKeyJoin,
   UIReturnKeyNext,
   UIReturnKeyRoute,
   UIReturnKeySearch,
   UIReturnKeySend,
   UIReturnKeyYahoo,
   UIReturnKeyDone,
   UIReturnKeyEmergencyCall,
} UIReturnKeyType;

默认为"返回"其他人是Go,谷歌,雅虎。

here

为了捕获返回事件,您可以使用textView Delegate方法

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
     if ([text isEqualToString:@"\n"]) {
        NSLog(@"Pressed Return Key");
    } else {
       NSLog(@"Pressed Any Other Key");
       }
return YES;
}

答案 1 :(得分:0)

UITextField有你可以实现的委托方法,当你按下键盘上的return时会调用它: - (BOOL)textFieldShouldReturn:(UITextField *)textField 您可以添加新行char" \ n"到textView文本,它将转到下一行。

UITextView当你按回车键时,它会像工作那样工作。

答案 2 :(得分:0)

enter image description here

点击并在此处设置您的关键字

 [textField setReturnKeyType:UIReturnKeyDone];

用于键盘实现类中的协议,设置

textfield.delegate = self;


  - (BOOL)textFieldShouldReturn:(UITextField *)textField {

    // write you code here what you want .

        return YES;
    }

如果你想要UITextView How to dismiss keyboard for UITextView with return key?

,请参考

答案 3 :(得分:0)

以下是在键盘上添加自定义按钮的方法。但苹果可以拒绝你的应用程序,所以要小心。

- (void)viewWillAppear:(BOOL)animtated {

    // Register the observer for the keyboardWillShow event
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animtated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    // create custom buttom
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(self.view.frame.size.width- 100, self.view.frame.size.height - 200.0f, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton addTarget:self action:@selector(addParaGraph:) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setBackgroundColor:[UIColor redColor]];
    [doneButton setTitle:@"Add Paragraph" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];

    // locate keyboard view
    UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView *keyboard;
    for (int i = 0; i < [tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        //if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) {
            [keyboard addSubview:doneButton];
        //}
    }
}

- (void)addParaGraph:(id)sender{

    // Write here you code to add new paragraph
}

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    // Set the FirstResponder of the UITextField on the layout
    [theTextField resignFirstResponder];
    return YES;
}

答案 4 :(得分:0)

Swift 3

let addressTextField = UITextField()
addressTextField.placeholder = "Search or enter website name"
addressTextField.layer.borderColor = UIColor.gray.cgColor
addressTextField.layer.borderWidth = 1.0
addressTextField.returnKeyType = .go
view.addSubview(addressTextField)
addressTextField.delegate = self

extension YourNaneController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}