Max*_*Max 1 delegates ios uicontainerview swift
在iOS中,我正在使用Swift构建应用程序.我有一个视图,其中设置了容器视图,链接嵌入视图.这是使用Storyboard设置的.
如何在Swift代码中的视图之间设置委托关系,以便我可以在另一个视图中发送消息/触发器功能?
任何帮助,将不胜感激!
假设您有两个视图ViewA和ViewB
实例ViewB是在内部创建的ViewA,因此ViewA可以向ViewB's实例发送消息,但是为了反过来我们需要实现委托(以便使用委托ViewB's实例可以向ViewA发送消息)
请按照以下步骤实施委派
1)在ViewB创建协议中
protocol ViewBDelegate{
func delegateMethod(controller:ViewB, text:String)
}
Run Code Online (Sandbox Code Playgroud)
2)在sender类中声明委托
class ViewB: UIView {
var delegate: ViewBDelegate! = nil
}
Run Code Online (Sandbox Code Playgroud)
3)使用类中的方法将委托方法调用为
@IBAction func callDelegateMethod(sender : UIBarButtonItem) {
delegate!. delegateMethod(self, text: colorLabel.text)
//assuming the delegate is assigned otherwise error
}
Run Code Online (Sandbox Code Playgroud)
4)采用ClassA中的协议
class ViewA: UIView, ViewBDelegate {
Run Code Online (Sandbox Code Playgroud)
5)实现委托
func delegateMethod(controller: ViewB, text: String) {
label.text = "The text is " + text
}
Run Code Online (Sandbox Code Playgroud)
6)设置代表
override func anyFuction()
{
// create ViewB instance and set the delegate
viewB.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
注意:这只是两个类之间快速授权的粗略概念,您可以根据自己的要求进行自定义.
| 归档时间: |
|
| 查看次数: |
1608 次 |
| 最近记录: |