Fattaneh Talebi
Fattaneh Talebi

Reputation: 767

Updating an instance variable for an object from another class in Swift

There are two UIViewController classes. One of them is a UITableViewController which reloads its data when needUpdating variable is true.
This variable should be updated in another class.

I make an instance from it and change this variable but I think that as this instance is not the one that is running nothing changes, so needUpdating is not updated.

class A: UITableViewController{
    var needUpdating: Bool = false
    override func viewWillAppear(animated: Bool) {
       ...
       if needUpdating {
           tableView.reloadData()
       }
    }
}
class B: UIViewController {
    override func viewWillAppear(animated: Bool) {
       ...
       var a = A()
       if ... {
           a.needUpdating = true
       }
    }
}

These two UIViewController are unrelated. They are not connected using segue

Upvotes: 0

Views: 1936

Answers (4)

Mina
Mina

Reputation: 2212

This is the first viewController:

class ViewController: UIViewController, ViewControllerSecondDelegate {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Next" {
        let vc = segue.destination as? ViewControllerSecond
        vc?.delegate = self
     }
   }
   func secondDelegate(_ value: Bool) {
      print("delegate") //needUpdating = value
    }
 }

and this is the secondViewController:

protocol ViewControllerSecondDelegate {
    func secondDelegate(_ value: Bool)
}

class ViewControllerSecond: UIViewController {
   weak var delegate: ViewControllerSecondDelegate?

   override func viewDidDisappear(_ animated: Bool) {
       super.viewDidDisappear(animated)
       delegate?.secondDelegate(true) //call this wherever you need it.
    }
}

Upvotes: 2

PGDev
PGDev

Reputation: 24341

enter image description here

You can use any type of segue according to your requirement. I have used Present Modally segue to present controller B

class A: UITableViewController
{
    var needUpdating: Bool = false
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        //...
        if needUpdating
        {
            tableView.reloadData()
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "BController"
        {
            let controller = segue.destination as! B
            controller.a = self
        }
    }
}
class B: UIViewController
{
    var a = A()

    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        //...
        if ...
        {
            a.needUpdating = true
        }
    }
}

Also you need to add UITableViewDataSource methods to make it work.

Upvotes: 0

Salman Ghumsani
Salman Ghumsani

Reputation: 3657

Every object has its own properties so here You are creating a new one instance:

class B: UIViewController {
    override func viewWillAppear(animated: Bool) {
       var a = A()
       if ... {
           a.needUpdating = true
       }
    }
}

var a = A()

You have to pass your old instance do not create new instance var a = A()

You can pass you old instance something like this:

class A: UITableViewController{
    var needUpdating: Bool = false
    override func viewWillAppear(animated: Bool) {
       ...
       if needUpdating {
           tableView.reloadData()
       }
    }
    //WHEN YOU PUSHED TO B ASSIGN THE CLASS A INSTANCE IN B CLASSS
    func pushToB {
         //PUSH STUFF
        var b = B()
        b.a = self

    }
}
class B: UIViewController {
    var a:A?
    override func viewWillAppear(animated: Bool) { 
       if ... {
           a?.needUpdating = true
       }
    }
}

Passed the instance like this:

let instance = self.storyboard?.instantiateViewControllerWithIdentifier("B")as! B
instance.a = self
self.navigationController?.pushViewController(instance, animated: true)

Upvotes: 1

Irshad Ahmad
Irshad Ahmad

Reputation: 1383

In your Appdelegate Class make a variable of UITableViewController

'var tableViewController: A?'

In your Class A make this Changes

'

let appDelegate = UIApplication.shared.delegate as! AppDelegate
    class A: UITableViewController{
        var needUpdating: Bool = false
        override func viewWillAppear(animated: Bool) {
           ...
            appDelegate.tableViewController = self
           if needUpdating {
               tableView.reloadData()
           }
        }
    }'

In Your Class B use AppDelegate variable reference instead of creating new instance

'class B: UIViewController {
    override func viewWillAppear(animated: Bool) {
       ...
       var a = appDelegate.tableViewController
       if ... {
           a.needUpdating = true
       }
    }
}'

Upvotes: 0

Related Questions