在Swift中从另一个类更新对象的实例变量

时间:2017-06-13 05:27:27

标签: ios swift

有两个UIViewController类。其中一个是UITableViewController,当needUpdating变量为真时重新加载其数据 该变量应该在另一个类中更新。

我从中创建一个实例并更改此变量,但我认为由于此实例不是正在运行的实例,因此needUpdating未更新。

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

这两个UIViewController无关。它们未使用segue

连接

4 个答案:

答案 0 :(得分:2)

这是第一个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
    }
 }

这是第二个ViewController:

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

答案 1 :(得分:1)

每个对象都有自己的属性,所以在这里您正在创建一个新的实例:

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

var a = A()

您必须传递旧实例不创建新实例 var a = A()

你可以传递这样的旧实例:

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

传递了这样的实例:

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

答案 2 :(得分:0)

在你的Appdelegate类中创建一个UITableViewController的变量

'var tableViewController: A?'

在A班中进行此更改

'

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

在您的B类中使用AppDelegate变量引用而不是创建新实例

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

答案 3 :(得分:0)

enter image description here

您可以根据自己的要求使用任何类型的segue。我使用Present Modally segue来呈现控制器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
        }
    }
}

此外,您还需要添加UITableViewDataSource方法才能使其正常工作。