user7684436
user7684436

Reputation: 717

Swift remove reference to view controller

I'm adding a view controller programatically via a navigation controller as I need to access the navigation bar whilst the controller is being presented.

I'm currently doing this via the method below however I need to be able to remove the reference to the view controller when the user taps the dismiss button.

let settingsController = storyboard?.instantiateViewController(withIdentifier: "SettingsController") as! SettingsController
            settingsController.view.alpha = 0
            settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height)
            self.view.addSubview(settingsController.view)
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                print(self.view.frame.height)
                settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height - 142)
                settingsController.view.alpha = 1
            }, completion: nil)

Is there a way I can do this as the current self.dismiss or self.popViewController doesn't work?

Thanks

UPDATE

I tried to implement this solution using the standard perform segue (below) by presenting modally option as I need to keep the navigation bar active. The settings controller height is less than the view frame. This didn't work so I'm trying the above method.

    self.definesPresentationContext = true
    self.modalPresentationStyle = .overCurrentContext
    self.modalTransitionStyle = .coverVertical
    self.performSegue(withIdentifier: "SettingsSegue", sender: nil)

When I call the settings controller up and dismiss another settings controller gets added resulting in multiple settings controllers. This is my issue.

Upvotes: 0

Views: 2343

Answers (2)

Scriptable
Scriptable

Reputation: 19750

Once the variable goes out of scope and the view controller is dismissed it should be de-referenced automatically and garbage collected as Swift uses ARC (automatic reference counting)

ARC basically means that Swift will keep track of the instance of that view controller and when it is no longer used it will de-allocate it and mark it for garbage collection.

You shouldn't need to do anything manually. Maybe showing some more of your code will help explain the issue further.

EDIT: Whilst the current answer does technically solve your issue. You are adding the ViewControllers view over the current view, the preferred way to do this is to push a view controller onto the navigation stack or segue to another view controller. doing it this way ensures that the view is deallocated when no longer required. With your current solution you are working around the navigation controller rather than using it as intended.

Upvotes: 0

Piyush Sinroja
Piyush Sinroja

Reputation: 160

var settingsController : SettingsController!

@IBAction func btnNext(_ sender: Any) {
    settingsController  = storyboard?.instantiateViewController(withIdentifier: "SettingsController") as! SettingsController
    settingsController.view.alpha = 1
    settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height)
    self.view.addSubview(settingsController.view)
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        print(self.view.frame.height)
        self.settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height - 142)
        self.settingsController.view.alpha = 1
    }, completion: nil)

}

@IBAction func btnDismiss(_ sender: Any) {
        self.settingsController.view.removeFromSuperview()
}

Upvotes: 1

Related Questions