【问题标题】:Hide Button if TextFields are Empty Equation Not Working如果文本字段为空,则隐藏按钮 方程式不起作用
【发布时间】:2021-11-21 16:43:41
【问题描述】:

我有 2 个不同的文本字段,如果这些文本字段为空,我想隐藏按钮,并在填充时使该按钮可见。我做了一个如下等式。第一次开机没有出现这个按钮,但是我填数据的时候也没有出现。哪里错了?

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

我的故事板: Storyboard

【问题讨论】:

  • 何时评估此条件?请分享更多代码
  • 创建一个minimal reproducible example。我们甚至不知道您使用的 UI 框架。
  • 感谢您的推荐,我再次更新了代码并添加了图像。

标签: swift


【解决方案1】:

viewDidLoad 中的代码仅执行一次。相反,您需要在每次更改文本时检查文本。

要实现这一点,您可以将视图控制器添加为UITextField 委托,如UITextView data change swift 中所述

快速而肮脏的解决方案是:

@objc func klavyeyiKapat() {
    view.endEditing(true)
    if isimTextField.text!.isEmpty == true && notTextField.text!.isEmpty == true {
        lokasyonuKaydet.isHidden = true
    } else {
        lokasyonuKaydet.isHidden = false
    }
}

【讨论】:

  • 谢谢,感谢您的留言,我通过一些更改解决了这个问题。我正在分享我的解决方案,如果有错误,如果您更正,我会很高兴。
【解决方案2】:

我添加了一个新的委托UITextFieldDelegate

我在 viewDidLoad 中做了定义

isimTextField.delegate = self
notTextField.delegate = self

我创建了一个新函数

 func updateButtonVisibility() {
    if isimTextField.text!.isEmpty == true && notTextField.text!.isEmpty == true {
        lokasyonuKaydet.isHidden = true
      } else {
        lokasyonuKaydet.isHidden = false
      }
  }

添加委托方法:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    updateButtonVisibility()
    return true
}

再次viewDidLoad

updateButtonVisibility()

在这些过程之后,它开始工作。如有错误请指正

【讨论】:

  • didBeginEditing 是错误的检查方法。此时文本值尚未更改。 textField(_:shouldChangeCharactersIn:replacementString:) 会更好,因为它会调用每个更改的文本
  • 我明白你的意思,但我不知道如何实现这一点,我还是个初学者。你能对我的代码做一个小的修改让我看看吗?
  • 我更新了你的答案
  • 谢谢,但是当我填写第一个文本字段时,按钮变为活动状态,当我删除数据时,按钮仍然存在。我想当我更专业的时候最好试试这个:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
相关资源
最近更新 更多