- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我开始研究 this question应用程序。我从类别的 tableView 开始:
对于数据交换,我决定使用一个协议(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
}
}
对我来说,显然没问题。但是模拟器上什么也没有出现。
我的问题是:如果容器用来呈现另一个 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
中的 ViewControllerCategoriesViewController
中的方法 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(...)
)。
对于这个简单的例子,使用来自类属性 value
的 didSet
的委托(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/
我有一个用 Swift 编写的自定义转换,当呈现的 View 从侧面进入时,被关闭的 View 消失。 现在我想要同样的效果,但我希望呈现的 View 从顶部进入,而被取消的 View 从底部出来。
该方法的预期目的是什么 findBinding(View view) ? 我一直在使用数据绑定(bind)库测试版。 目前还没有针对各个类的官方引用文档,所以我一直在查看源代码以了解我们可以访问哪些方
我试图在遍历 &str 时查看当前位置前面的字符。 let myStr = "12345"; let mut iter = myStr.chars().peekable(); for c in ite
每当我在 Azure 中创建新的 Azure 函数应用时,我都会选择一个存储帐户供其使用。 创建应用后,如何查看/更改存储帐户? 我尝试在门户中浏览,但无法看到可以在哪里配置它。 最佳答案 创建后,您
我想查看读取 QFile 的 QTextStream 的下一个字符,以创建一个高效的标记器。 但是,我没有找到任何令人满意的解决方案。 QFile f("test.txt"); f.open(QIOD
当我旋转手机时, ListView 中选定的项目将被取消选择。这是我的适配器。我只想更改所选项目的颜色(仅是单个选择),当我旋转手机时,我想查看我选择的内容。现在,当我旋转手机时,我看不到所选项目。
我开始编写代码来检查函数的返回值,但我不确定在捕获到一些错误后如何继续。 例如,在 fgets 中: while( fgets( rta, 3, stdin ) == NULL ) {
是否可以获取或创建一个 optional 轮,它以假想的圆圈滚动而不是直接向下(垂直)滚动? 直线链接但想要弯曲 例如就像控件 here ,但车轮是弯曲的? 最佳答案 有没有可能你想要的是一个轮播?
当我尝试为其创建 View 时出现错误:“ View 的 SELECT 在 FROM 子句中包含一个子查询”,但它在普通查询中工作正常。我认为它与我的 WHERE 子句有关,但我不明白为什么或如何修复
在一个类中,我有以下代码: /// [System.Xml.Serialization.XmlElementAttribute("Errors", typeof(ErrorsType))] [Sys
我想显示来自 catch block 的错误(日志)消息。如何在单个屏幕上显示所有消息(堆栈),以便用户了解? 谢谢... 最佳答案 使用 Toast 怎么样? 示例: Toast.makeText(
我有以下屏幕(图像),当按下按钮时显示一个侧面菜单,菜单出现,另外我有一个 uitableview 来显示其他东西 我的问题是,当侧边菜单出现时,uitableview 被调整了大小。 如何保持 ui
invariant violation element type is invalid: expected a string (for built-in components) or a class/
我是新手,正在尝试学习 fork() 和系统调用的功能,现在我正在使用 execvp() 来尝试制作 bash,但我遇到的问题是,当我编写正确的命令时,程序结束,我想循环使用我的 bash,直到有人在
我正在尝试使用 Laravel request validation method 创建一个登录页面 和凭据验证 -> 如果用户未通过身份验证,它将返回错误“密码错误....” 我在两种不同的情况下看
我有一个 .jar 文件,我需要查看 jar 文件的内容。 是否有任何解码器可用于解码 jar 文件。 我也有 solaris 环境,我需要知道什么是 最佳答案 使用jar命令行,即 jar tf j
Git 提供了几种方式可以帮你快速查看提交中哪些文件被改变。 如果你每天使用 Git,应该会提交不少改动。如果你每天和其他人在一个项目中使用 Git,假设 每个人 每天的提
问题 tensor详细数值 不能直接print打印: ? 1
一,uptime 可以查看系统的运行时间 show global status like 'uptime'; 二,利用linux命令查看 Linux 下有个 uptime 可以查看系统的运行时
在所有主流的浏览器中,均能够查看原始的 XML 文件 不要指望 XML 文件会直接显示为 HTML 页面 查看 XML 文件 <?xml version="1.0" e
我是一名优秀的程序员,十分优秀!