Привет, я новичок в swift, и мне было интересно, как отобразить цифровую клавиатуру, когда я нажимаю на текстовое поле. Я хочу, чтобы текстовое поле получало пользовательский ввод с цифровой клавиатуры и преобразовывало это значение см в дюймы. Это то, что он показывает на симуляторе до сих пор Все примеры, которые я нашел в сети, не сработали ... Мне кажется, что я получаю эту ошибку, потому что я использую общие переменные в 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.
}
}