gpt4 book ai didi

ios - Controller 之间的委托(delegate)不起作用。为什么?

转载 作者:行者123 更新时间:2023-11-30 11:36:38 25 4
gpt4 key购买 nike

我尝试使用委托(delegate)将数据从 Detail2(ViewController) 中的 textField 发送到 ViewController 中的数组。我在这里使用了打印方法,第一个打印显示一个元素已添加到数组中,但位于 ViewVillAppear() 下面的第二个打印方法显示数组为空。如何?我希望能够使用委托(delegate)将数据添加到我的表中。

["sdsd"] 首先从控制台打印[]从控制台第二次打印

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var add: UIBarButtonItem!
@IBOutlet var tv: UITableView!
var array :[String] = []

override func viewDidLoad() {
super.viewDidLoad()

tv.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let vc: Detail2 = segue.destination as! Detail2
vc.delegate = self
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = array[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
array.remove(at: indexPath.row )
tv.reloadData()
}
}

func alert () {
}



override func viewWillAppear(_ animated: Bool) {
tv.reloadData()
print(array)

}

}

extension ViewController: Data {

func tekst(data: String) {
array.append(data)
print(array)

}
}

详细信息2

    protocol Data {
func tekst (data: String)
}

class Detail2: UIViewController {

var delegate: Data? = nil
@IBAction func btn(_ sender: Any) {

let sb = storyboard?.instantiateViewController(withIdentifier: "Main" ) as! ViewController
navigationController?.pushViewController(sb, animated: true)

if delegate != nil {
if txtfield.text != nil {

let napis = txtfield.text
delegate?.tekst(data: napis!)
}
}
}
@IBOutlet var btn: UIButton!
@IBOutlet var txtfield: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

btn.backgroundColor = UIColor.blue
btn.tintColor = UIColor.white
btn.layer.cornerRadius = 25
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

只需更新您的函数:

@IBAction func btn(_ sender: Any) {
if delegate != nil {
if txtfield.text != nil {
let napis = txtfield.text
delegate?.tekst(data: napis!)
}

navigationController?.popViewController(animated: true)

}
}

更新:

extension ViewController: Data {

func tekst(data: String) {
array.append(data)
print(array)
self.tv.reloadData()

}
}

关于ios - Controller 之间的委托(delegate)不起作用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693761/

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