Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从自定义视图类向UIViewController添加自定义视图_Ios_Swift_Custom View - Fatal编程技术网

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]