gpt4 book ai didi

ios - 在 TableViewController 和 ViewController 之间进行委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 14:13:50 25 4
gpt4 key购买 nike

我在 iOS 开发的第一周就遇到了在 View Controller 之间传递数据的问题。我的设置包括一个带有 VC 的 View ,其中有一个按钮,还有一个容器 View (没有关联的 View Controller )。容器 View 使用 TableViewController 嵌入了到 TableView 的 Segue。该表有 6 行,每行都有一个步进器,可以更改关联行上 TextView 的值。我想做的是在我按下主视图上的按钮时收集所有 TextView 的值。

我正在尝试使用委托(delegate)来执行此操作,但是当我按下按钮时,返回值始终为零。我认为问题在于 VC 没有通过 prepareForSegue 函数传递给 TableView Controller ,但我不确定为什么?可能与 Controller 的加载顺序有关?

import UIKit

class PredictionViewController: UIViewController, PredictionDelegate {

var predictionData: String!

@IBOutlet weak var messageTextBox: UITextView!
@IBOutlet weak var predictionSubmitButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

self.messageTextBox.isEditable = false
}

override func viewDidAppear(_ animated: Bool) {

}

func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "predictionSegue") {
// pass data to next view
let vc = segue.destination as! PredictionsTableViewController
vc.predictionHomeDelegate = self
}
}

func receiveData(with data: String) {
predictionData = data
print(predictionData)
}

@IBAction func predictionSubmitButtonAction(_ sender: UIButton) {
print(predictionData)
}

}

TableViewController:(剥离到最小)

import UIKit

protocol PredictionDelegate: class {
func receiveData(with data: String)
}

class PredictionsTableViewController: UITableViewController, PredictionDelegate {

weak var predictionHomeDelegate: PredictionDelegate?

@IBOutlet weak var homeTeamScore1: UITextView!
@IBOutlet weak var homeTeamStepper1: UIStepper!

override func viewDidLoad() {
super.viewDidLoad()

}

override func viewDidAppear(_ animated: Bool) {

}

func getPredictionList() {
//does some stuff
self.passDataBackwards()
}

func receiveData(with data: String) {}

func passDataBackwards() {
let data = "{\"score\":\"1\"}"
predictionHomeDelegate?.receiveData(with: data)
}

@IBAction func homeTeamStepper1Action(_ sender: UIStepper) {

let score = Int(sender.value).description
homeTeamScore1.text = score
self.passDataBackwards()
}
}

storyboard view

感谢收到的任何帮助!

最佳答案

编辑:

评论后...

您对协议(protocol)和委托(delegate)有错误的看法。这里不需要它们。

相反,在您的“家庭”VC 中,获取对嵌入式 VC 的引用。然后,在您的嵌入式 VC 中添加一个函数,您可以调用该函数来获取其数据。

enter image description here

// "home" view controller
class PredictionViewController: UIViewController {

// this will be a reference to the embedded view controller
var embeddedVC: PredictionsTableViewController?

@IBAction func getDataButtonTapped(_ sender: Any) {
if let tableData = embeddedVC?.getMyData() {
print("Result: \(tableData)")
}
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "predictionSegue") {
if let vc = segue.destination as? PredictionsTableViewController {
// get a reference to the embedded VC
self.embeddedVC = vc
}
}
}

}

// embedded view controller
class PredictionsTableViewController: UIViewController {

var numTaps = 0

func getMyData() -> String {
return "\(numTaps)"
}

@IBAction func didTap(_ sender: Any) {
numTaps += 1
}

}

你很接近,但有几个错误...

这是一个非常非常简单的例子。带容器的 View Controller ,它有一个带按钮的 View Controller 。

代码:

import UIKit

// your protocol
protocol PredictionDelegate: class {
func receiveData(with data: String)
}

// embedded view controller
// NOTE: this should NOT include PredictionDelegate
class PredictionsTableViewController: UIViewController {

weak var predictionHomeDelegate: PredictionDelegate?

@IBAction func didTap(_ sender: Any) {
// on button tap, "send data back"
predictionHomeDelegate?.receiveData(with: "Test")
}

}

// "home" view controller
// NOTE: this DOES include PredictionDelegate
class PredictionViewController: UIViewController, PredictionDelegate {

func receiveData(with data: String) {
print(data)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "predictionSegue") {
if let vc = segue.destination as? PredictionsTableViewController {
// set self as the delegate of the embedded PredictionsTableViewController
vc.predictionHomeDelegate = self
}
}
}

}

注意事项:

  • 不要在您的嵌入式 View Controller 中包含 func receiveData(with data: String) {}
  • 不要将 PredictionDelegate 分配给您的嵌入式 View Controller

关于ios - 在 TableViewController 和 ViewController 之间进行委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52296808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com