简体   繁体   English

Swift:将 popUp 中的数据添加到 struct 并在 tableview 中显示

[英]Swift: add data from popUp to struct and show in tableview

I'm building an app to keep track of scores i have a struct我正在构建一个应用程序来跟踪我有一个结构的分数

   */
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)

}

as you can see i have two int arrays that will hold the user points.如您所见,我有两个 int arrays 将保存用户点。 I have a controller with two tableviews to show the array of points added by the user, i'm going to skip some of the code since i know is not needed for my problem, this is my Main ViewController where tables will show the points我有一个 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
        }
    }
}

} }

now here is where my problem occurs, when i segue to my pop up and the user enters the score needed and taps done, the data doesn't append to the array and my tableview won't reload, i've tried many different ways, using callbacks, delegates, i tried userdefaults since is not very important data but nothing seems to work, and i'm stuck, this is my pop up view controller button action where it should happen, i left the textfield.text in the parameter for reference现在这是我的问题发生的地方,当我继续弹出我的弹出窗口并且用户输入所需的分数并点击完成时,数据不会 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 
}

any help will be appreciated, thank you.任何帮助将不胜感激,谢谢。

It seems that you declare a separate instance inside the popup您似乎在弹出窗口中声明了一个单独的实例

 gameScore.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)

You need to set a delegate to the real object in segue您需要在 segue 中将委托设置为真正的 object

let popUpView = segue.destination as! PopUpViewController
popUpView.delegate = self

And declare it inside the popup like并在弹出窗口中声明它

var delegate:GameScoreViewController?

Then use it然后使用它

delegate?.addPoints(teamA: Int(pointsTextField.text!)!, teamB: 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM