制作单位转换应用,单击文本字段时无法获得数字键盘

问题描述 投票:-1回答:1

嗨,我是新手,我想知道当我单击文本字段时如何显示数字键盘。我希望一个文本字段从数字键盘获取用户输入并将该cm值转换为英寸。 This is what it's showing on simulator so far我在网上找到的所有示例均无法正常工作。.我觉得自己收到此错误是因为我在AppDelegate.swift中使用共享变量

所以这就是我到目前为止的内容:

//AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    //Variables being shared
    var cmValue:Double = 1.0

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

//FirstViewController.swift
import UIKit

class FirstViewController: UIViewController {

    @IBOutlet weak var dataTextField: UITextField!

    //make object so that we can access AppDelegate
    let ap = UIApplication.shared.delegate as! AppDelegate
    //called when printing page
    override func viewWillAppear(_ animated: Bool) {
        //set shared variable value to text field
        dataTextField.text = String(ap.cmValue)
    }

    @IBAction func tapInput() {
        //exit keyboard
        if let text = dataTextField.text {
            //if there is a value in text field
            if let cmValue = Double(text) {
                ap.cmValue = cmValue 
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }




}
ios swift xcode
1个回答
1
投票

以在模拟器中显示键盘,当您在文本字段内按下时,按Command + k显示键盘以查看您的文本字段正在使用哪个键盘。如果要更改键盘类型,只需在文本字段上更改键盘类型属性

类似这样:

dataTextField.keyboardType = .numberPad
© www.soinside.com 2019 - 2024. All rights reserved.