Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS view container: how to set the delegate

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:

like image 668
Billy Pilgrim Avatar asked Dec 03 '25 15:12

Billy Pilgrim


2 Answers

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
}
like image 102
Ian MacDonald Avatar answered Dec 05 '25 03:12

Ian MacDonald


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

enter image description here

Second set your delegate to embededVC in prepareForSegue Function

enter image description here

Finally in your EmbededViewController call hide function to MainViewController enter image description here

like image 23
Cruz Avatar answered Dec 05 '25 03:12

Cruz



Donate For Us

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