繁体   English   中英

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

[英]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.

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