在模拟器中点击Textfield无法工作
我正在使用Google Maps SDK,目前我有一张地图可将用户从他们的任何地方发送到特定位置。我希望用户能够输入他们想要去的地方,而不是总是被路由到相同的位置。我在GUI中添加了一个文本字段,每当我在模拟器中运行该应用程序并单击文本字段时,就不会出现键盘,并且就好像我没有敲击它一样。在模拟器中点击Textfield无法工作
这里是如何我已经建立了我的项目:
import UIKit
import GoogleMaps
import GooglePlaces
import CoreData
class ViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate
{
let locationManager = CLLocationManager()
var currLocation: CLLocationCoordinate2D!
var keyy = "myKeyIsHere"
var didFindMyLocation = false
var count: Int = 0
var button = UIButton()
var latitude = UITextField()
var longitude = UITextField()
var path = GMSMutablePath()
super.viewDidLoad()
let blah = GMSCameraPosition.camera(withLatitude: 39.8282, longitude: -98.5795, zoom: 25.0)
mapView.camera = blah
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
currLocation = mapView.myLocation?.coordinate
button = UIButton(type: .system)
button.frame = CGRect(x: 135, y: 40, width: 100, height: 40)
button.backgroundColor = UIColor.clear
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.blue.cgColor
button.setTitle("Route", for: UIControlState.normal)
button.addTarget(self, action: #selector(ViewController.buttonPressed), for: UIControlEvents.touchUpInside)
latitude = UITextField()
latitude.frame = CGRect(x: 5, y: 90, width: 175, height: 35)
latitude.backgroundColor = UIColor.white
latitude.layer.cornerRadius = 5
latitude.layer.borderWidth = 1
latitude.layer.borderColor = UIColor.black.cgColor
latitude.isUserInteractionEnabled = true
latitude.text = " Enter Latitude"
latitude.keyboardType = UIKeyboardType.numbersAndPunctuation
self.latitude.delegate = self
longitude = UITextField()
longitude.frame = CGRect(x: 195, y: 90, width: 175, height: 35)
longitude.backgroundColor = UIColor.white
longitude.layer.cornerRadius = 5
longitude.layer.borderWidth = 1
longitude.layer.borderColor = UIColor.black.cgColor
longitude.isUserInteractionEnabled = true
longitude.text = " Enter Longitude"
longitude.keyboardType = UIKeyboardType.numbersAndPunctuation
self.longitude.delegate = self
self.view.addSubview(latitude)
self.view.addSubview(button)
self.view.addSubview(longitude)
}
还有更多,但所有这一切,我觉得是相关的问题。我实际上在这里添加了两个文本字段,但都没有工作!请帮忙!
您需要从故事板或以编程方式启用Control State of your textField
。
latitude.isEnabled = true
longitude.isEnabled = true
更新:
注:把你所有的代码中viewDidLoad?
像下面
override func viewDidLoad() {
super.viewDidLoad()
}
试试下面的方法,以使textField.When之一textField开始编辑...
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("textFieldShouldBeginEditing:")
return true
}
func textFieldDidBeginEditing(_ textField: UITextField){
print("textFieldDidBeginEditing:")
}
我实际上只做了一个按钮,它被称为“时间”。我已经添加了该行,但它仍然不起作用!任何其他想法? – skyleguy
在代码中写入了textfield委托方法吗?然后检查textFieldShouldBeging函数,如果你写了代码来退出该文本字段的键盘。除此之外,我没有看到任何原因,即使您通过编码编写了文本字段声明,也没有显示键盘。
当您以编程方式在您的课程中编写了所有元素时,请检查是否有任何元素覆盖了您的文本字段。这就是为什么即使你认为你点击文本框,你实际上是点击了其他东西。
不,我没有写任何委托方法!他们是什么? – skyleguy
请复制粘贴您的完整视图的方法和负责UI设计的任何相关方法。 –
请将所有按钮和textfields声明从'viewDidLoad'方法移动到'viewDidLayoutSubviews'方法。如果您使用的是自动布局,那么在视图中编写按钮和文本字段声明确实会导致问题。
一种有趣的问题。你在模拟器中测试它吗? –
我尝试过使用模拟器,并使用我自己的手机! – skyleguy
由于我在视图控制器中创建了所有的GUI元素,我应该完全删除main.storyboard吗? – skyleguy