gpt4 book ai didi

ios - 委托(delegate)未在 Swift 中创建

转载 作者:行者123 更新时间:2023-11-30 13:19:16 27 4
gpt4 key购买 nike

我正在开发一个涉及代理协议(protocol)的 swift 项目。我有两个文件在这方面工作顺利,但是我遇到了一个问题,即委托(delegate)在不同的两个文件中不断出现为零,而我在其中使用不同的名称执行与以前相同的操作。似乎由于某种原因没有设置/创建委托(delegate)。请参阅下面的代码:

ItemDetailViewController

import UIKit

protocol ItemDetailViewControllerDelegate: class {

func itemDetailViewControllerDidCancel(controller: ItemDetailViewController)
func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: ChecklistItem)
func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: ChecklistItem)
}

class ItemDetailViewController: UITableViewController, UITextFieldDelegate {

**weak var delegate: ItemDetailViewControllerDelegate?**
@IBOutlet weak var doneBarButton: UIBarButtonItem!
@IBOutlet weak var textField: UITextField!
var itemToEdit: ChecklistItem?


@IBAction func cancel() {
delegate?.itemDetailViewControllerDidCancel(self)
}

@IBAction func done() {
if let item = itemToEdit {
item.text = textField.text!
delegate?.itemDetailViewController(self, didFinishEditingItem: item)
}
else {
let item = ChecklistItem()
item.text = textField.text!
delegate?.itemDetailViewController(self, didFinishAddingItem: item)
}
}

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
return nil
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
textField.becomeFirstResponder()
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let oldText: NSString = textField.text!
let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string)
doneBarButton.enabled = (newText.length > 0)
return true
}

override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
if let item = itemToEdit {
title = "Edit Item"
textField.text = item.text
doneBarButton.enabled = true
}
}
}

ChecklistViewController

import UIKit

class ChecklistViewController: UITableViewController, ItemDetailViewControllerDelegate {

var checklist: Checklist!

override func viewDidLoad() {
super.viewDidLoad()
title = checklist.name
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return checklist.items.count
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let item = checklist.items[indexPath.row]
performSegueWithIdentifier("ShowMap", sender: item)
tableView.deselectRowAtIndexPath(indexPath, animated: true)

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem", forIndexPath: indexPath)
let item = checklist.items[indexPath.row]
configureTextForCell(cell, withChecklistItem: item)
return cell
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// 1
checklist.items.removeAtIndex(indexPath.row)
// 2
let indexPaths = [indexPath]
tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
}

func configureTextForCell(cell: UITableViewCell,withChecklistItem item: ChecklistItem) {
let label = cell.viewWithTag(1000) as! UILabel
label.text = item.text
}

func itemDetailViewControllerDidCancel(controller: ItemDetailViewController) {
dismissViewControllerAnimated(true, completion: nil)
}

func itemDetailViewController(controller: ItemDetailViewController,didFinishEditingItem item: ChecklistItem) {
if let index = checklist.items.indexOf(item) { // indexOf needs to compare (test for equality) item to the items in the array
let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
configureTextForCell(cell, withChecklistItem: item)
}
}
dismissViewControllerAnimated(true, completion: nil)

}

func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: ChecklistItem) {
let newRowIndex = checklist.items.count
checklist.items.append(item)
let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
let indexPaths = [indexPath]
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
dismissViewControllerAnimated(true, completion: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
if segue.identifier == "AddItem" {
let navigationController = segue.destinationViewController
as! UINavigationController
let controller = navigationController.topViewController
as! ItemDetailViewController
controller.delegate = self
}
else if segue.identifier == "EditItem" {
let navigationController = segue.destinationViewController
as! UINavigationController
let controller = navigationController.topViewController
as! ItemDetailViewController
controller.delegate = self

if let indexPath = tableView.indexPathForCell(
sender as! UITableViewCell) {
controller.itemToEdit = checklist.items[indexPath.row]
}
}
}
}

最佳答案

ChecklistViewController:prepareForSegue 中的 segue 标识符是否可能不正确,或者在 Storyboard中设置不正确?如果是这样,目标 Controller 委托(delegate)将不会被设置。

关于ios - 委托(delegate)未在 Swift 中创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926975/

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