1

I'm new to iOS development, but I'm having an issue with one of the views that I'm working on. I have a UIDatePicker that can either be hidden or visible depending on the state of a UISwitch. It seems that the associated @IBAction does not trigger when the view starts out hidden. It does work when the date picker starts out visible, so the IBAction is working.

Here's a simplified version of my code:

import UIKit

class StatusEditorViewController: UIViewController {
    
    @IBOutlet var expiryPicker: UIDatePicker!
    @IBOutlet var enableExpirySwitch: UISwitch!
    
    var editingObject: StoredStatus?
    
    private var pickerIsVisible = false
    private var expiresIn: TimeInterval?
    
    override func viewDidLoad() {
        // Set a default value
        expiryPicker.countDownDuration = TimeInterval(3600)
        
        // If this view got passed an object to edit, use that for expiresIn 
        if let status = editingObject {
            if let expires = status.expiresIn.value {
                expiresIn = TimeInterval(expires)
            }
        }
        
        // Hide the picker and turn off the "enable expiry" switch if we don't 
        // have a value yet. We'll show the picker once the switch has been pressed
        pickerIsVisible = expiresIn != nil
        enableExpirySwitch.isOn = expiresIn != nil
        
        updatePicker()
    }
    
    func updatePicker() {
        expiryPicker?.isHidden = !pickerIsVisible
    }
    
    @IBAction func expiryDidUpdate(_ sender: UIDatePicker) {
        expiresIn = sender.countDownDuration
        print(expiresIn!)
    }
    
    
    @IBAction func expirySwitchDidUpdate(_ sender: UISwitch) {
        pickerIsVisible = sender.isOn
        updatePicker()
        
        // If the user just turned on the switch, we want to make sure we store the 
        // initial value already, in case the user navigated away
        if (sender.isOn && expiresIn == nil) {
            expiresIn = expiryPicker.countDownDuration
        }
    }
 
}

I'm not sure what's going wrong. I tried manually attaching a target (e.g. self.expiryPicker.addTarget(self, action: #selector(setExpiryValue), for: .allEditingEvents)) once the view becomes available, but that didn't work either.

I hope someone can tell me what I'm doing wrong. I'm guessing there's something fundamental that I'm doing wrong, but so far no search on Google or SO has led me to the answer.

Thanks in advance

f.w.i.w, I'm running XCode 11.7, with Swift 5, with a deployment target of iOS 13.7

Marco
  • 625
  • 6
  • 24
  • 1
    Can you post a project on GitHub to demonstrate the issue? – matt Nov 05 '20 at 11:15
  • @matt I've put it here: https://github.com/marcohamersma/ios-debug. Turns out, I was wrong. It's the first edit to UIDatePicker that gets ignored. For either case. The hidden+visible was a red herring, it appears. I'll update the question. – Marco Nov 05 '20 at 12:17
  • If [this answer is still up to date](https://stackoverflow.com/a/42685845/847689), my approach is flawed, and I should just read the data out when leaving the view. That seams cleaner anyway. This question has become a bit messy, perhaps it's better to remove it. – Marco Nov 05 '20 at 12:29

0 Answers0