[英]How to add protocol type as subview
So I wrote a simple protocol: 所以我写了一个简单的协议:
protocol PopupMessageType{
var cancelButton: UIButton {get set}
func cancel()
}
and have a customView: 并有一个customView:
class XYZMessageView: UIView, PopupMessageType {
...
}
and then I currently have: 然后我现在有:
class PopUpViewController: UIViewController {
//code...
var messageView : CCPopupMessageView!
private func setupUI(){
view.addSubview(messageView)
}
}
But what I want to do is: 但是我想做的是:
class PopUpViewController: UIViewController {
//code...
var messageView : PopupMessageType!
private func setupUI(){
view.addSubview(messageView) // ERROR
}
}
ERROR I get: 错误我得到:
Cannot convert value of type 'PopupMessageType!' 无法转换类型为'PopupMessageType!'的值! to expected argument type 'UIView' 到预期的参数类型“ UIView”
EDIT: I'm on Swift 2.3! 编辑:我在Swift 2.3上!
Change the type of property messageView to (UIView & PopupMessageType)! 更改属性消息查看类型(的UIView和PopupMessageType)!
I mean 我的意思是
class PopUpViewController: UIViewController {
//code...
var messageView : (UIView & PopupMessageType)!
private func setupUI(){
view.addSubview(messageView) // ERROR
}
}
In Swift 4 you can do this: 在Swift 4中,您可以执行以下操作:
typealias PopupMessageViewType = UIView & PopupMessageType
And then use PopupMessageViewType
as the type of the variable. 然后使用PopupMessageViewType
作为变量的类型。
DISCLAIMER: I do not have the swift 2.3 compiler anymore since swift 4 is the new normal for iOS development. 免责声明:我已经没有Swift 2.3编译器了,因为Swift 4是iOS开发的新常态。 The following code may possibly need tweaks to get it working in swift 2.3 以下代码可能需要调整才能使其在Swift 2.3中正常工作
Essentially we will be making a 2x1 mux where the two inputs are the same object. 本质上,我们将制作2x1多路复用器,其中两个输入是同一对象。 The output depends on whether you set the mux to choose the first or the second one. 输出取决于您是否将多路复用器设置为选择第一个或第二个。
// The given protocol
protocol PopupMessageType{
var cancelButton: UIButton {get set}
func cancel()
}
// The object that conforms to that protocol
class XYZMessageView: UIView, PopupMessageType {
var cancelButton: UIButton = UIButton()
func cancel() {
}
}
// The mux that lets you choose the UIView subclass or the PopupMessageType
struct ObjectPopupMessageTypeProtocolMux<VIEW_TYPE: UIView> {
let view: VIEW_TYPE
let popupMessage: PopupMessageType
}
// A class that holds and instance to the ObjectPopupMessageTypeProtocolMux
class PopUpViewController: UIViewController {
var messageWrapper : ObjectPopupMessageTypeProtocolMux<UIView>!
private func setupUI(){
view.addSubview(messageWrapper.view)
}
}
//...
let vc = PopUpViewController() // create the view controller
let inputView = XYZMessageView() // create desired view
// create the ObjectPopupMessageTypeProtocolMux
vc.messageWrapper = ObjectPopupMessageTypeProtocolMux(view: inputView, popupMessage: inputView) //<-- 1
vc.messageWrapper.view // retreive the view
vc.messageWrapper.popupMessage.cancel() // access the protocol's methods
vc.messageWrapper.popupMessage.cancelButton // get the button
1) I input the "inputView" twice for the initializer of ObjectPopupMessageTypeProtocolMux. 1)我为ObjectPopupMessageTypeProtocolMux的初始化程序输入了两次“ inputView”。 They are the same class instance, but they get casted to different types. 它们是相同的类实例,但是它们被强制转换为不同的类型。
I hope this helps you get to where you wanna go in swift 2.3 我希望这可以帮助您快速到达想要的地方2.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.