gpt4 book ai didi

ios - Swift 委托(delegate)没有调用

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

我目前正在编写程序,但我一直遇到委托(delegate)问题。所以我退后一步,编写了一个较小的程序,只是为了测试委托(delegate)功能并尝试理解这个概念,但我仍然无法让它工作,我已经浏览了这个页面并尝试了我所看到的每一个建议,但什么也没有似乎正在发挥作用。这个简单的程序有一个导航按钮,可以打开一个新的 View Controller 。在此 Controller 上,我有一个名为“保存”的导航按钮。当我按下“保存”按钮时,它应该调用委托(delegate)函数并将某些内容打印到控制台,但它不打印。这是我正在使用的代码。

import UIKit

class ListViewContrroller: UITableViewController,
AddItemViewControllerDelegate {



let CellIdentifier = "Cell Identifier"
var items = [Item]()

// Mark: Initialization
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)

// Load Items

}

override func viewDidLoad() {
super.viewDidLoad()

title = "Items"
print(items)

// Register Class
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier)

// Create Add Button
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addItem))

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return items.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeue reusable Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Fetch Item
let item = items[indexPath.row]
// Configure Table View Cell
cell.textLabel?.text = item.itemName

return cell
}

//Mark Helper Methods

// Mark: Add Item button pushed
@objc func addItem(sender: UIBarButtonItem) {
//print("New Item button was pressed")
performSegue(withIdentifier: "AddItemViewController", sender: self)
}

// Mark: -
// Mark: Delegate Methods
func testDelegates() {
print("hi")

}

// MARK: - Navigation

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "AddItemViewController" {
if let navigationController = segue.destination as? UINavigationController,
let addItemViewController = navigationController.viewControllers.first as? AddItemViewController {
addItemViewController.delegate = (self as AddItemViewControllerDelegate)
}
}
}

}

和我的第二个 View Controller

import UIKit

protocol AddItemViewControllerDelegate {
func testDelegates()
}
class AddItemViewController: UIViewController {


var delegate: AddItemViewControllerDelegate?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}



// MARK: - Navigation



// Mark -
// Mark: Actions
@IBAction func cancel(sender:UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}

@IBAction func save(sender: UIBarButtonItem) {

// Notify Delegate
print("Pre-Delegate message")
delegate?.testDelegates()
dismiss(animated: true, completion: nil)

}

最后我的控制台看起来像这样

[]
Pre-Delegate message

这表明我的数组为空并且正在识别保存按钮。我们将非常感谢您提供的任何帮助

最佳答案

Prepare for Segue 函数不正确。我有

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

当我需要的时候

override func prepare(for segue: UIStoryboardSegue, sender: Any?)

关于ios - Swift 委托(delegate)没有调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52993686/

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