Ios 从自定义视图类向UIViewController添加自定义视图
我有一个自定义视图。我命名为Ios 从自定义视图类向UIViewController添加自定义视图,ios,swift,custom-view,Ios,Swift,Custom View,我有一个自定义视图。我命名为AnimatingView。我已经在AnimatingView.swift中自定义了视图。在ViewController中,我使用 let customAnimatingView = AnimatingView(overView: self.view) 然后我用- self.view.addSubview(customAnimatingView) 但是,我不希望用户像上面那样将其添加为子视图。我想从AnimatingView调用一个func,自定义视图应该像控制器
AnimatingView
。我已经在AnimatingView.swift
中自定义了视图。在ViewController中,我使用
let customAnimatingView = AnimatingView(overView: self.view)
然后我用-
self.view.addSubview(customAnimatingView)
但是,我不希望用户像上面那样将其添加为子视图。我想从AnimatingView
调用一个func,自定义视图应该像控制器一样添加它-
customAnimatingView.preset()
有谁能给我一些提示,告诉我如何实现这种行为。在自定义视图中
// add a property that refers to the the vc container of the view
// init it when you create an instance of the view
weak var parentController:UIViewController?
func present() {
parentController?.view.addSubview(self)
}
或者如果您需要发送主视图
var parentView:UIView?
func present() {
parentView?.addSubview(self)
}
在自定义视图中,您可以执行以下操作:
// add a property that refers to the the vc container of the view
// init it when you create an instance of the view
weak var parentController:UIViewController?
func present() {
parentController?.view.addSubview(self)
}
或者如果您需要发送主视图
var parentView:UIView?
func present() {
parentView?.addSubview(self)
}
搜索容器视图搜索容器视图我真的想到了它。但是,我害怕生成保留圆,因为我的自定义视图实际上是progressView。我担心在完成处理程序中,我对控制器的引用可能会创建一个保留循环。这就是我在属性中添加
弱
,并在完成中使用[weak self]
的方式我实际上想到了它。但是,我害怕生成保留圆,因为我的自定义视图实际上是progressView。我担心在完成处理程序中,我对控制器的引用可能会创建一个保留圆。这就是我在属性中添加弱
,并在完成中使用[weak self]