【问题标题】:How can i change action of the button "return" on the keyboard on Xcode?如何更改 Xcode 键盘上“返回”按钮的操作?
【发布时间】:2015-05-04 12:59:33
【问题描述】:

我需要更改键盘上“返回”按钮的文本,我什至需要更改它的操作。 动作应该是一个新的段落。 你能帮帮我吗?

【问题讨论】:

  • UITextFieldUITextView 需要它吗?
  • 可以在 UITextField 的 Delegate 方法中编写代码.. -(BOOL)textFieldShouldReturn;并且可以通过IB属性更改返回按钮的名称
  • 您也想更改文本“return”吗?在键盘中
  • 我使用了 UITextfield,我想更改文本“返回”

标签: ios text keyboard return action


【解决方案1】:

您不能将返回按钮重命名为任何自定义文本

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

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

默认为“返回”,其他为 Go、Google、Yahoo。

here

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

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

【讨论】:

    【解决方案2】:

    UITextField 有这个你可以实现的委托方法,当你在键盘上按下回车键时会调用它: - (BOOL)textFieldShouldReturn:(UITextField *)textField 您可以在 textView 文本中添加一个新行字符“\n”,它将转到下一行。

    UITextView 应该是这样工作的,当你按下 Return 键时它会换行。

    【讨论】:

      【解决方案3】:

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

       [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?,请参考这里

      【讨论】:

      • 选择您的文本字段并继续他的财产
      【解决方案4】:

      这是在键盘上添加自定义按钮的方法。但苹果可能会拒绝您的应用,所以要小心。

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

      【讨论】:

        【解决方案5】:

        斯威夫特 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
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-11
          • 2012-10-21
          • 1970-01-01
          • 1970-01-01
          • 2017-03-09
          • 1970-01-01
          • 2011-08-25
          • 1970-01-01
          相关资源
          最近更新 更多