更改从父ViewController嵌入TableViewController的数据

时间:2016-07-01 16:48:03

标签: ios swift uitableview uiviewcontroller

我试图让用户按下顶部的其中一个名字。当他们这样做时,子TableViewController将更改布尔值currentUserIsLoggedInUser

storyboard

在我的FactsController我有代码:

class FactsController: UIViewController {

    @IBOutlet weak var currentUserName: UIButton!
    @IBOutlet weak var friendUserName: UIButton!

    var currentPersonSelectedIsCurrentUser: Bool = false

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ChangePersonSegue") {

            let embed: FactsTableViewController = segue.destinationViewController as! FactsTableViewController
            embed.currentPersonSelectedIsCurrentUser = self.currentPersonSelectedIsCurrentUser
            embed.refresh //this doesn't call the function like id want it to
        }
    }

    func changeFactList() {
        //segue for table view controller for ___
        // call prepareForSegue
        performSegueWithIdentifier("ChangePersonSegue", sender: self)
    }

    override func viewDidLoad() {
        //changethings(user)
    }

    @IBAction func tappedUserName (sender: UIButton) {
        if currentPersonSelectedIsCurrentUser {

        }
        else {
            currentPersonSelectedIsCurrentUser = true
            changeFactList()
            currentUserName.titleLabel?.textColor = UIColor.lightGrayColor()
            friendUserName.titleLabel?.textColor = UIColor.darkGrayColor()
        }
    }

    @IBAction func tappedFriendName (sender: UIButton) {
        if currentPersonSelectedIsCurrentUser {
            currentPersonSelectedIsCurrentUser = false
            changeFactList()
            // change text color
            currentUserName.titleLabel?.textColor = UIColor.darkGrayColor()
            friendUserName.titleLabel?.textColor = UIColor.lightGrayColor()
        }
        else {

        }
    }
}

然后在我的FactsTableViewController我有代码:

class FactsTableViewController: UITableViewController {

    let cellID = "factCell"

    var currentUserIsLoggedInUser: Bool = false

    //var currentUserName: String = ""
    var facts = [Fact]()
    var currentUser: User!
    var soUser: User!

    let ref: FIRDatabaseReference = FIRDatabase.database().reference()

    override func viewDidLoad() {
        if currentUserIsLoggedInUser {
            // make tableview for the current user
        }
        else {
            // make tableview for the friend         
        }
    }

    func refresh() {
        self.tableView.reloadData()
    }

}

如果有一种方法可以在按下按钮时从FactsController发送一个布尔值,这将使一切变得更加容易。

1 个答案:

答案 0 :(得分:2)

如果你想:

  

一种方法,只需在按钮出现时从FactsController发送一个布尔值   压

然后你需要一个代表。虽然故事板并没有让你对这种情况有很大的控制权。我在nib文件中移动您的UI并手动添加FactsTableViewController作为子VC。拥有这些类的实例,您可以更轻松地管理代理。