Ios 使用委托创建UIViewController
我有一个Ios 使用委托创建UIViewController,ios,swift,Ios,Swift,我有一个ViewController,它调用一个自定义类PopOverModal,并传入它自己和一个子ViewController class ViewController: UIViewController { ... @IBAction func testPopOver(_ sender: Any) { var childVC = // initialize child view controller with view from storyboard
ViewController
,它调用一个自定义类PopOverModal
,并传入它自己和一个子ViewController
class ViewController: UIViewController {
...
@IBAction func testPopOver(_ sender: Any) {
var childVC = // initialize child view controller with view from storyboard
var myPopOverModal = PopOverModal(parent: self, child: childVC);
}
...
}
我需要childVC将一些任务委托给PopOverModal
class ChildViewController: UIViewController {
...
weak var delegate: PopOverModal?
...
}
class PopOverModal {
init(parent: UIViewController, child: UIViewController) {
child.delegate = self
child.dismiss(animated: true, completion: nil)
}
}
PopOverModal的初始值设定项出现错误,因为UIViewController没有属性委托
于是我做了以下几件事:
protocol PopOverViewController: class {
weak var delegate: PopOverModal? {get set}
}
class ChildViewController: UIViewController, PopOverViewController {
...
weak var delegate: PopOverModal?
...
}
class PopOverModal {
init(parent: UIViewController, child: PopOverViewController) {
child.delegate = self
child.dismiss(animated: true, completion: nil)
}
}
但是,现在我得到一个错误,discouse
不是PopOverViewController的函数
class ViewController: UIViewController {
...
@IBAction func testPopOver(_ sender: Any) {
var childVC = // initialize child view controller with view from storyboard
var myPopOverModal = PopOverModal(parent: self, child: childVC);
}
...
}
在确保ChildViewController继承UIViewController的同时,如何访问UIViewController和PopOverViewController的函数和属性
我知道我可以创建一个从UIViewController继承并符合PopOverViewController的混合类,并让我的ChildViewController继承它,但是还有其他方法吗?如果保证
child
将是ChildViewController
类的实例,那么您可以修改初始值设定项签名,使其接受相应的对象:
init(parent: UIViewController, child: ChildViewController) {
child.delegate = self
child.dismiss(animated: true, completion: nil)
}
如果它可以是任意UIViewController,但委托
仅在ChildViewController
的实例中才是实际的,则可以使用类型转换和可选链接或可选绑定:
// Option #1 - optional chaining
init(parent: UIViewController, child: UIViewController) {
(child as? ChildViewController)?.delegate = self
child.dismiss(animated: true, completion: nil)
}
// Option #2 - optional binding
init(parent: UIViewController, child: UIViewController) {
if let child = child as? ChildViewController {
child.delegate = self
}
child.dismiss(animated: true, completion: nil)
}
顺便说一句,使用基于协议的委派被认为是一种最佳实践。下面是一个如何实现的示例:
protocol PopOverDelegate: class {
// add any methods and properties that the delegate should implement
func didPressButton()
}
class ChildViewController: UIViewController {
...
weak var delegate: PopOverDelegate?
...
// call delegate methods where you need them
func onButtonPress() {
self.delegate?.didPressButton()
}
}
class PopOverModal: PopOverDelegate {
init(parent: UIViewController, child: UIViewController) {
(child as? ChildViewController)?.delegate = self
child.dismiss(animated: true, completion: nil)
}
// MARK: - delegate methods
func didPressButton() {
print(#function)
}
}