【发布时间】:2017-06-22 13:06:45
【问题描述】:
我正在学习深入了解委托模式。 iOS中很多代码示例使用了两个ViewControllers,其中涉及prepare(for segue:...)。
我希望我的程序仅使用一个带有委托协议的 ViewController,但不使用 segue 或情节提要。 ViewController 有一个按钮来执行一个简单的委托方法,比如说添加一个数字。
ViewController 类:
class ViewController: UIViewController, theDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
// It is here I got stuck
// How do I set delegate = self without out involving segue or the storyboard at all? Do I need to instantizate the dedecated delegate class and how?
// To conform to delegate -- theDelegate
func add(num: Int) {
// Output result on ViewController
}
func minus(num: Int) {
// Output result on ViewController
}
}
专用Delegate类:
protocol theDelegate: class {
func add(num: Int)
func minus(num: Int)
}
class ClassDelegate: NSObject {
weak var delegate: theDelegate?
func x() {
delegate?.add(num: 100)
}
}
【问题讨论】:
标签: swift delegates viewcontroller