我試圖讓用戶按頂部的名稱之一。當他們這樣做時,子TableViewController將更改布爾值currentUserIsLoggedInUser
。從父ViewController更改嵌入的TableViewController的數據
在我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
發一個按鈕被按下時的布爾值,這會使一切變得更容易。
它不會更改布爾值,它是一個全新的視圖控制器 – Wain
您是否希望其他其他ViewController知道前一個控制器發生了什麼並且它應該更改其布爾值? –