All, my question is a follow up to this one: SWIFT - Hide a view container with a button in the ViewContainer
In short, I have a UIViewController (Main) that contains a button "SHOW" and a containerView. The container view ("containerView") has a single button "HIDE". Initially the containerView is hidden, and when the "SHOW" button is tapped, it is displayed. All is well. I want to use a delegate such that when the "HIDE" button is tapped, the containerView will be hidden.
I understand all the delegate stuff, EXCEPT how does the containerView become a viewController so that I can assign Main to be the delegate?
The container view is a UIView which (somehow) points to / contains a view controller, but I cannot determine how to reference it.
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")
}
}
output: +Show -Show +hideButtonTapped -hideButtonTapped
TIA, :bp:
It doesn't need to be a view controller in order to allow assignment of a delegate. The delegate is just a property of an object. Simply do this in your view controller:
override func viewDidLoad() {
super.viewDidLoad()
pickerContainerView.delegate = self
pickerContainerView.hidden = true
}
If your ViewController have Any ContainerView, then ViewController call prepareForSegue after awakeFromNib
this called before viewDidLoad
so you can set delegate in
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
First
Set your EmbededSegue Identifier

Second set your delegate to embededVC in prepareForSegue Function

Finally in your EmbededViewController call hide function to MainViewController

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With