[英]Swift: add data from popUp to struct and show in tableview
我正在構建一個應用程序來跟蹤我有一個結構的分數
*/
var teamA_name: String!
var teamB_name: String!
var teamA_points: [Int] = []
var teamB_points: [Int] = []
/*
- Add points to the teams
*/
mutating func addPoints(teamA: Int, teamB: Int){
self.teamA_points.append(teamA)
self.teamB_points.append(teamB)
}
如您所見,我有兩個 int arrays 將保存用戶點。 我有一個 controller 有兩個表格視圖來顯示用戶添加的點數組,我將跳過一些代碼,因為我知道我的問題不需要,這是我的主視圖控制器,表格將顯示點
class GameScoreViewController: UIViewController {
/*
- Properties
*/
var gameScore = GameScore()
override func viewDidLoad() {
super.viewDidLoad()
//- Setup delegates & datasources
teamA_tableview.delegate = self
teamA_tableview.dataSource = self
teamB_tableview.delegate = self
teamB_tableview.dataSource = self
// - Button configuration
addPointsButton.layer.cornerRadius = 5
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPopUp"{
let popUpView = segue.destination as! PopUpViewController
// this is where i call my popup view
}
}
}
}
現在這是我的問題發生的地方,當我繼續彈出我的彈出窗口並且用戶輸入所需的分數並點擊完成時,數據不會 append 到數組並且我的表格視圖不會重新加載,我嘗試了很多不同的方法,使用回調,委托,我嘗試了 userdefaults 因為不是很重要的數據,但似乎沒有任何工作,我被卡住了,這是我的彈出視圖 controller 按鈕操作應該發生的地方,我在參數中留下了 textfield.text以供參考
@IBAction func addBtnPressed(_ sender: Any) {
// this is the func to append data to the array
gameScore.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)
self.dismiss(animated: true, completion: nil)
//after dismissed it should reload table view or insert row with the user entered score
}
任何幫助將不勝感激,謝謝。
您似乎在彈出窗口中聲明了一個單獨的實例
gameScore.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)
您需要在 segue 中將委托設置為真正的 object
let popUpView = segue.destination as! PopUpViewController
popUpView.delegate = self
並在彈出窗口中聲明它
var delegate:GameScoreViewController?
然后使用它
delegate?.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.