-2

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

2 Answers2

0

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
    }
}

ManWithBear
  • 2,787
  • 15
  • 27
  • Thanks, I solved the problem by making a few changes thanks to your message. I am sharing my solution, if there is an error, I would be happy if you correct it. – Newbie Swift Coder Sep 29 '21 at 19:39
0

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

ManWithBear
  • 2,787
  • 15
  • 27
  • `didBeginEditing` is wrong method for check. At this point text value hasn't changed yet. `textField(_:shouldChangeCharactersIn:replacementString:)` would be better place, since it called on every text changed – ManWithBear Sep 29 '21 at 19:46
  • I understand what you mean but I have no idea how to implement this, I'm a beginner yet. Can you make a small adaptation with my code for me to see? – Newbie Swift Coder Sep 29 '21 at 19:48
  • I updated your answer – ManWithBear Sep 29 '21 at 19:54
  • Thank you, but when I fill the first textfield, the button becomes active, and when I delete the data, the button remains. I guess I'd better try this when I'm much more pro :) – Newbie Swift Coder Sep 29 '21 at 20:15