gpt4 book ai didi

swift - 在 Swift 中使用 Container 查看另一个 ViewController 时按协议(protocol)传递数据

转载 作者:可可西里 更新时间:2023-11-01 00:52:10 27 4
gpt4 key购买 nike

我开始研究 this question应用程序。我从类别的 tableView 开始:

enter image description here

对于数据交换,我决定使用一个协议(protocol):

protocol Category {
func data(object:AnyObject)
}

在第一个ViewController中有如下代码:

class ViewController: UIViewController {

var items:[String] = ["Desktop","Tablet","Phone"]

let CategoriesData:Category? = nil

override func viewDidLoad() {
super.viewDidLoad()

CategoriesData?.data(items)
}

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

在第二个ViewController(Container中的tableView)中有如下代码:

class CategoriesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, Category  {

@IBOutlet var table: UITableView!

var items:[String] = []

func data(object: AnyObject) {
self.items = (object as? [String])!
print(object)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:TableViewCell = self.table.dequeueReusableCellWithIdentifier("SegueStage") as! TableViewCell

cell.nameLabel.text = items[indexPath.row]
return cell
}
}

对我来说,显然没问题。但是模拟器上什么也没有出现。

enter image description here

我的问题是:如果容器用来呈现另一个 viewController 作为通过协议(protocol)传递数据应该完成

最佳答案

已编辑

我回答了为什么 TO:s 解决方案没有按预期工作,但我刚刚意识到我还没有给出一个可行的答案来说明如何使用协议(protocol)作为 ViewController 的委托(delegate) -> ViewController 通信。在有人可能更好地回答完整问题之前,我会在下面留下一半的答案。


在代码中使用 protocol 的方式中,您将协议(protocol) Category 定义为 instances 类型的委托(delegate)> View Controller 。当类型 ViewController 的实例在其他类的范围内初始化时,因此在其他类的范围内本地拥有,该实例可以将回调委托(delegate)给所属类。

问题是您的 CategoriesViewController 不包含类型 ViewController 的任何实例。我们注意到这两个类本身都是 UIViewController 的子类,但它们都不包含彼此的实例。因此,您的 CategoriesViewController 确实符合协议(protocol) Category,通过实现协议(protocol)方法 data(...),但是没有 CategoriesViewController 中的 ViewController 实例,可以对此函数进行回调。因此,您的代码编译文件,但实际上,永远不会调用 CategoriesViewController 中的方法 data(...)

我可能弄错了,但据我所知,协议(protocol)委托(delegate)用于在模型(对于 MVC 设计中的模型)和 Controller (参见下面的示例)之间进行回调,而在您的情况下,您希望直接在模型之间进行委托(delegate)两个 Controller 。


作为模型-委托(delegate)- Controller 设计的一个例子,考虑一些自定义用户控件,具有一些关键属性 value(例如评级控件中的位置),作为 UIView< 的子类实现:

// CustomUserControl.swift
protocol CustomUserControlDelegate {
func didChangeValue(value: Int)
}

class CustomUserControl: UIView {

// Properties
// ...
private var value = 0 {
didSet {
// Possibly do something ...

// Call delegate.
delegate?.didChangeValue(value)
}
}

var delegate: CustomUserControlDelegate?

// ... some methods/actions associated with your user control.
}

现在假设您的 CustomUserControl 实例用于 View Controller ,例如 ViewController。您可以在 View Controller 中使用自定义控件的委托(delegate)函数来观察 CustomUserControl 模型中的关键变化,就像您使用 UITextFieldDelegate 的固有委托(delegate)函数一样> 对于 UITextField 实例(例如 textFieldDidEndEditing(...))。

对于这个简单的例子,使用来自类属性 valuedidSet 的委托(delegate)回调来告诉 View Controller 它的一个导出已经关联了模型更新:

// ViewController.swift
Import UIKit
// ...

class ViewController: UIViewController, CustomUserControlDelegate {

// Properties
// ...
@IBOutlet weak var customUserControl: CustomUserControl!
// Instance of CustomUserControl in this UIViewController

override func viewDidLoad() {
super.viewDidLoad()
// ...

// Custom user control, handle through delegate callbacks.
customUserControl.delegate = self
}

// ...

// CustomUserControlDelegate
func didChangeValue(value: Int) {
// do some stuff with 'value' ...
}

}

关于swift - 在 Swift 中使用 Container 查看另一个 ViewController 时按协议(protocol)传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34224625/

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