【问题标题】:Disable iphone keyboard button禁用 iphone 键盘按钮
【发布时间】:2012-09-02 02:16:28
【问题描述】:

有没有办法禁用 iPhone 键盘上的“,”按钮,或者至少阻止用户在我的 UITextField 中输入“,”?

【问题讨论】:

    标签: ios


    【解决方案1】:

    是的。设置文本字段的委托,然后实现以下方法:

    - (BOOL)textField:(UITextField *)tf shouldChangeCharactersInRange:(NSRange)r replacementString:(NSString *)str
    {
        if (tf == theTextFieldYouWannaBehaveLikeThis) {
            if ([str isEqualToString:@","]) {
                return NO;
            }
        }
        return YES;
    }
    

    【讨论】:

    • 是的,但是现在如果我从用户界面委托,我所有的 UItextFields 都有这个,即使只有 1 个必须有(其他有不同的功能)
    • @Alessandro 为什么您认为发件人文本字段将自身作为第一个参数传递?您可以检查它是否等于特定的文本字段。 (见编辑)
    【解决方案2】:

    UITextField 有一个 keyboardType 属性:

    typedef enum {
        UIKeyboardTypeDefault,                // Default type for the current input method.
        UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
        UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
        UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
        UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
        UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
        UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
        UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    
        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
    
    } UIKeyboardType;
    

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 2021-10-21
      • 1970-01-01
      • 2012-06-21
      • 2012-09-15
      • 2015-06-07
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多