我目前正在编写程序,但是我一直遇到委托问题。因此,我退后一步,编写了一个较小的程序,只是为了测试委托人的功能并尝试理解该概念,但仍然无法使它正常工作。我浏览了此页面,并尝试了所见过的每条建议,但没有做任何事情似乎正在工作。这个简单的程序有一个导航按钮,它会弹出一个新的视图控制器。在此控制器上,我有一个名为“保存”的导航按钮。当我按下“保存”按钮时,它应该调用委托函数并将某些内容打印到控制台,但不会打印。这是我正在使用的代码。
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
哪个显示我的数组为空并且正在识别保存按钮。您可以提供的任何帮助将不胜感激
答案 0 :(得分:0)
“准备缝合”功能不正确。我有
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
我需要的时候
override func prepare(for segue: UIStoryboardSegue, sender: Any?)