【问题标题】:IOS: action with enter key of iPad KeyBoardIOS:使用 iPad KeyBoard 的回车键操作
【发布时间】:2011-08-23 05:13:32
【问题描述】:

我有两个文本字段,在第一个文本字段中我写“Hello”,当我在 iPad 键盘上按 Enter 时,我希望在第二个文本字段中出现“世界”;如何使用 enter 在我的应用程序中创建操作?

【问题讨论】:

    标签: objective-c xcode ios ipad uitextfield


    【解决方案1】:

    您通常会将您的视图控制器分配为文本字段的委托,然后实现 textFieldShouldReturn: 方法,例如:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        otherTextField.text = @"World"
        return YES;
    }
    

    【讨论】:

    • 啊好吧我没写 textFiled.delegate = self;
    • 在 Swift 中的足迹是 func textFieldShouldReturn(textField: UITextField) -> Bool
    【解决方案2】:

    您可以通过在控制器中实现UITextFieldDelegate 协议来做到这一点。例如,您可以执行以下操作:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        if (textField == theFirstTextField && [textField.text isEqualToString:@"Hello"]) {
            theSecondTextField.text = @"World";
        }
        return YES;
    }
    

    【讨论】:

      【解决方案3】:

      将您的视图控制器设置为文本字段的委托,然后实现

      -(BOOL)textFieldShouldReturn:(UITextField *)textField
      

      当按下键盘上的 enter 按钮时调用它。

      【讨论】:

        【解决方案4】:

        这大致就是你要做的。调整设备类型的条件(如果你真的只想要 iPad):

        #pragma mark - UITextField Delegate
        
        - (BOOL)textFieldShouldReturn:(UITextField *)textField 
        {
            if (textField == self.firstTextField && [textField.text isEqualToString:@"Hello"]) {
                self.secondTextField.text = @"World";
            }
            return YES;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-16
          • 2020-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-25
          • 1970-01-01
          相关资源
          最近更新 更多