I have 2 different textfields and I want to hide the button if these textfields are empty, and make this button visible when it is filled. I made an equation like below. The button does not appear on the first boot, but it does not appear when I fill in the data. Where is wrong?
var secilenLatitude = Double()
var secilenLongitude = Double()
@IBOutlet weak var isimTextField: UITextField!
@IBOutlet weak var notTextField: UITextField!
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var lokasyonuKaydet: UIButton!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(konumSec(gestureRecognizer:)))
mapView.addGestureRecognizer(gestureRecognizer)
let keyboardGR = UITapGestureRecognizer(target: self, action: #selector(klavyeyiKapat))
self.view.addGestureRecognizer(keyboardGR)
if isimTextField.text!.isEmpty == true && notTextField.text!.isEmpty == true {
lokasyonuKaydet.isHidden = true
} else {
lokasyonuKaydet.isHidden = false
}
}
@objc func klavyeyiKapat() {
view.endEditing(true)
}
@objc func konumSec(gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
let dokunulanNokta = gestureRecognizer.location(in: mapView)
let dokunulanKoordinat = mapView.convert(dokunulanNokta, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = dokunulanKoordinat
annotation.title = isimTextField.text
annotation.subtitle = notTextField.text
mapView.addAnnotation(annotation)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = CLLocationCoordinate2D.init(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)
let span = MKCoordinateSpan.init(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion.init(center: location, span: span)
mapView.setRegion(region, animated: true)
}
My Storyboard: Storyboard
CodePudding user response:
Code in viewDidLoad
executed only once. Instead you need to check for text every time text have been changed.
To achieve this you can add view controller as UITextField
delegate as described in UITextView data change swift
Quick and dirty solution would be:
@objc func klavyeyiKapat() {
view.endEditing(true)
if isimTextField.text!.isEmpty == true && notTextField.text!.isEmpty == true {
lokasyonuKaydet.isHidden = true
} else {
lokasyonuKaydet.isHidden = false
}
}
CodePudding user response:
I added a new delegate UITextFieldDelegate
I made definitions in viewDidLoad
isimTextField.delegate = self
notTextField.delegate = self
I created a new function
func updateButtonVisibility() {
if isimTextField.text!.isEmpty == true && notTextField.text!.isEmpty == true {
lokasyonuKaydet.isHidden = true
} else {
lokasyonuKaydet.isHidden = false
}
}
Add delegate method:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
updateButtonVisibility()
return true
}
Again viewDidLoad
:
updateButtonVisibility()
and after these processes it started working. Please correct me if i have mistakes