是否可以通过编程方式更改uitextfield的键盘类型,从而实现以下操作:

if(user is prompted for numeric input only)
    [textField setKeyboardType: @"Number Pad"];

if(user is prompted for alphanumeric input)
    [textField setKeyboardType: @"Default"];

推荐答案

对于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).
    UIKeyboardTypeDecimalPad,             // A number pad including a decimal point
    UIKeyboardTypeTwitter,                // Optimized for entering Twitter messages (shows # and @)
    UIKeyboardTypeWebSearch,              // Optimized for URL and search term entry (shows space and .)

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

} UIKeyboardType;

您的代码应为

if(user is prompted for numeric input only)
    [textField setKeyboardType:UIKeyboardTypeNumberPad];

if(user is prompted for alphanumeric input)
    [textField setKeyboardType:UIKeyboardTypeDefault];

Ios相关问答推荐

找不到com.google.vr:sdk-base:1.180.0

如何在react 本机模块中使用Bundle.main.Path()在SWIFT中导入assets资源

SwiftUI Divider过大了其父视图

有没有办法将滚动视图RTL(阿拉伯语)与Ltr动画一起使用?

iOS应用程序冻结在UICollectionView代码,但我没有';没有任何UICollectionViews

如何结合`@dynamicMemberLookup`和`ExpressibleByStringInterpolation`来实现方法链?

Swift Combine和iOS版本限制?

圆角矩形路径上的蛇形动画

iOS按钮和navigationLink在List中相邻

渲染的海边重定向在物理 iOS 设备上不起作用

Swift 图表:.chartYScale 似乎只适用于 100 的增量?

搜索在 ios xamarin.forms 中不起作用

如何 comments .plist 文件中的行?

Xcode - 错误 ITMS-90635 - Bundle 包中的 Mach-O 无效 - 提交到 App Store

如何更新推送通知服务证书

使用未知类型创建图像格式是一个错误与 UIImagePickerController

如何从 UIPickerView 获取选定的值

iOS SDK - 以编程方式生成 PDF 文件

比较没有时间分量的 NSDates

Flutter:如何创建一个新项目