UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

View Controller loses connection to constraints in IBOutlets when switching tabs in UITabView

Forums > Swift

OK, so here's the relevant bit of my code:

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var questionView: QuestionView!
    @IBOutlet weak var answerView: AnswerView!

    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var tableViewTopConstraint: NSLayoutConstraint! // Active at start, attaches tableView.topAnchor to questionView.bottomAnchor
    @IBOutlet weak var tableViewTopAlternateConstraint: NSLayoutConstraint! // Inactive at start, attaches tableView.topAnchor to answerView.bottomAnchor

    func submitAnswer() {
        tableViewTopConstraint.isActive = false
        tableViewTopAlternateConstraint = true
    }

    func newQuestion() {
        tableViewTopConstraint.isActive = true
        tableViewTopAlternateConstraint = false
    }
}

I'm building a question/answer type of app and the two subviews are different sizes for design reasons. I have it set so that it toggles between the two subviews depending on the current state (toggling back for a new question when the user taps to advance), and it works fine...

...until I go to a different tab on the UITabView that encloses everything (e.g. to change the settings then resume testing)

The moment the tab changes it's like the NSLayoutConstraint outlets no longer exist. The orders to change them still process (I've verified this in the console), but they do nothing.

  • I've tried declaring the outlets as strong (there was no difference in behavior)
  • I've removed the outlets and managed them in code (it worked the first time, but afterward it stretched the shorter QuestionView's height to match tableView's new top position instead of moving tableView up to the bottom of QuestionView as was intended)
  • I've tried implementing a UITabViewController so that I could call tabBar(_:didSelect:) and replace the view controller with a brand new instance of MyViewController() whenever I switch back to that tab (It loads the first time, but when I try to switch tabs it finds nil when accessing any of the IBOutlets...even though I didn't tap on the tab for the testing view and filtered by item.tag when replacing the existing view controller)

Any suggestions on how else to attack this problem?

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.