所有,我的问题是对此问题的跟进:SWIFT - Hide a view container with a button in the ViewContainer
简而言之,我有一个UIViewController(Main),它包含一个按钮" SHOW"和一个containerView。容器视图(" containerView")只有一个按钮" HIDE"。最初,containerView是隐藏的,当" SHOW"点击按钮,显示。一切都很好。我想使用一个代表,当" HIDE"点击按钮,将隐藏containerView。
我理解所有委托的东西,除了容器视图如何成为一个viewController,以便我可以指定Main作为委托?
容器视图是一个UIView(以某种方式)指向/包含视图控制器,但我无法确定如何引用它。
import UIKit
class MainViewController: UIViewController,dismissPickerViewDelegate {
@IBOutlet weak var pickerContainerView: containerView!
@IBOutlet weak var showButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
pickerContainerView.hidden = true
}
@IBAction func showButtonTapped(sender: AnyObject) {
println("+Show")
pickerContainerView.hidden = false
println("-Show")
}
func dismissPicker()
{
println("+dismissPicker")
println("-dismissPicker")
}
}
// ========================
import UIKit
protocol dismissPickerViewDelegate {
func dismissPicker()
}
class pickerViewController: UIViewController {
var delegate : dismissPickerViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var hideButton: UIButton!
@IBAction func hideButtonTapped(sender: AnyObject) {
println("+hideButtonTapped")
delegate?.dismissPicker()
println("-hideButtonTapped")
}
}
输出: +展会 -节目 + hideButtonTapped -hideButtonTapped
TIA, :沸点:
答案 0 :(得分:3)
它不需要是一个视图控制器,以便允许分配一个委托。委托只是对象的属性。只需在视图控制器中执行此操作:
override func viewDidLoad() {
super.viewDidLoad()
pickerContainerView.delegate = self
pickerContainerView.hidden = true
}
答案 1 :(得分:1)