iOS.& Swift Books uikit学徒

13
代表代表& Protocols 由Matthijs Hollemans撰写& Fahim Farook

您现在有开奖结果3d添加项目屏幕,显示键盘,允许用户输入文本。该应用程序也正确验证了输入,以便您永远不会最终包含空的文本。

But how do you get this text into a new ChecklistItem object that you can add to the items array on the Checklists screen? That is the topic that this chapter will explore.

添加新的CheckListItems.

为了使新的项目除了工作,你就必须得添加项目屏幕通知新添加项目的检查表视图控制器。这其中的根本任务,每开奖结果3diOS应用的需求做开奖结果3d:从开奖结果3d视图控制器将消息发送到另开奖结果3d。

使用项目数组向屏幕发送CheckListItem对象
使用项目数组向屏幕发送CheckListItem对象

凌乱的方式

锻炼: How would you tackle this problem? The done() method needs to create a new ChecklistItem object with the text from the text field (easy), then add it to the items array and the table view in ChecklistViewController (not so easy).

class AddItemViewController: UITableViewController, . . . {

  // This variable refers to the other view controller
  var checklistViewController: ChecklistViewController

  @IBAction func done() {
    // Create the new checklist item object
    let item = ChecklistItem()
    item.text = textField.text!

    // Directly call a method from ChecklistViewController
    checklistViewController.add(item)
  }
}
屏幕A知道所有关于屏幕B的信息,但是B一无所知
ylmoet u njejn ozh usior lzreob w,qen z jqelm futzecz af u

代表方式

你’ve already seen delegates in a few different places: the table view has a delegate that responds to taps on the rows; the text field has a delegate that you used to validate the length of the text; and the app also has things named AppDelegate and SceneDelegate — check the project navigator.

屏幕推出屏幕B并成为其代表
gvmuib i ceijvwiw fvmiih x ophh qezepov ipk jufageha

这是屏幕B看到的:只有代表部分,而不是屏幕的其余部分
MDU在XCIZ DNMAIS K Joab:UPMT QKA Sazesocu Qugg,var ryi fiwv iv brnium e

委托协议

➤在顶部 AddItemViewController.swift., add the following after the import line, but before the class line — it is not part of the AddItemViewController object:

协议 AddItemViewControllerDelegate: class {
  func addItemViewControllerDidCancel(
    _ controller: AddItemViewController)
  func addItemViewController(
    _ controller: AddItemViewController, 
    didFinishAdding item: ChecklistItem
  )
}

协议

在Swift,开奖结果3d 协议 她没有任何与计算机网络会议费。它只是一组方法的名称。

var delegate: AddItemViewControllerDelegate

通知代理人

你’re not done yet in AddItemViewController.swift.。 View Controller需要开奖结果3d属性它可以用于引用委托。

weak var delegate: AddItemViewControllerDelegate?
@IBAction func cancel() {
  delegate?.addItemViewControllerDidCancel(self)
}

@IBAction func done() {
  let item = ChecklistItem()
  item.text = textField.text!

  delegate?.addItemViewController(self, didFinishAdding: item)
}

术语

I mentioned a few times that variables and constants in Swift must always have a value. In other programming languages the special symbol nil or NULL is often used to indicate that a variable has no value. This is not allowed in Swift for normal variables.

weak var delegate: AddItemViewControllerDelegate?
delegate?.addItemViewControllerDidCancel(self)

符合委托协议

Before you can give AddItemViewController its delegate, you first need to make the ChecklistViewController suitable to play the role of delegate.

class ChecklistViewController: UITableViewController, AddItemViewControllerDelegate {
Xcode警告不符合协议
pzaya fidxh iraex kel Jucvucsuwx XO Ndobafes

// MARK: - Add Item ViewController Delegates
func addItemViewControllerDidCancel(
  _ controller: AddItemViewController
) {
  navigationController?.popViewController(animated: true)
}

func addItemViewController(
  _ controller: AddItemViewController, 
  didFinishAdding item: ChecklistItem
) {
  navigationController?.popViewController(animated: true)
}
// MARK: - Navigation
override func prepare(
  for segue: UIStoryboardSegue, 
  sender: Any?
) {
  // 1
  if segue.identifier == "AddItem" {
    // 2
    let controller = segue.destination as! AddItemViewController
    // 3
    controller.delegate = self
  }
}

设置Segue标识符

看到在上面的代码中提到SEGUE标识符?在哪里把它设置?答案是,它不是!我们需要设置标识,以使上面的代码工作。如果你忘记了,那么你就不会得到segueing到添加项目屏幕当代表正确设置。

命名清单场景和添加项目场景之间的SEGUE
Bequwt mjo pireo jemsuox mli qnenmgeqkl ygica ubj ypi ejt agaf bbebe

添加新的do do项目

➤ Change the implementation of the didFinishAdding delegate method in checklistviewcontroller.swift. to the following:

func addItemViewController(
  _ controller: AddItemViewController, 
  didFinishAdding item: ChecklistItem
) {
  let newRowIndex = items.count
  items.append(item)

  let indexPath = IndexPath(row: newRowIndex, section: 0)
  let indexPaths = [indexPath]
  tableView.insertRows(at: indexPaths, with: .automatic)
  navigationController?.popViewController(animated:true)
}
你 can finally add new items to the to-do list
Kao Sif Ciqujrh elb loh upozy yu rxe ka-tu buvk

wa

I still owe you an explanation about the weak keyword. Relationships between objects can be weak or strong. You use weak relationships to avoid what is known as an 所有权周期.

有开奖结果3d技术问题?想报告开奖结果3d错误吗? 您可以向官方书籍论坛中的书籍作者提出问题和报告错误 这里.

有反馈分享在线阅读体验吗? 如果您对UI,UX,高亮反馈,或者我们的在线读者的其他功能,你可以将它们发送给设计团队与下面的表格:

© 2021 Razeware LLC

您可以免费读取,本章的部分显示为 混淆了 文本。解开这本书,我们的书籍和视频的整个目录,具有raywenderlich.com专业订阅。

现在解锁

要突出或记笔记,您需要在订阅中拥有这本书或自行购买。