IOS Presentation Container: How to Install a Delegate

That's all, my question is the following: SWIFT - Hide the view container using the button in the ViewContainer

In short, I have a UIViewController (Main) that contains a "SHOW" button and a container. In the container view ("containerView") there is one button "HIDE". Initially, the View container is hidden, and when the "SHOW" button is pressed, it is displayed. Things are good. I want to use the delegate in such a way that when I click the HIDE button, the container will be hidden.

I understand all the delegate stuff, EXCEPT, how does the View container become a viewController so that I can assign Main to the delegate?

The view of the container is a UIView that (somehow) points to / contains the 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")
    }
}

Exit: + Show -Show + hideButtonTapped -hideButtonTapped

TIA,

+4
source share
2 answers

In order to allow the appointment of a delegate, he does not need to be a view controller. A delegate is just an object of an object. Just do it in your controller:

override func viewDidLoad() {
    super.viewDidLoad()
    pickerContainerView.delegate = self
    pickerContainerView.hidden = true
}
+3
source

If your ViewController has Any ContainerView, then the ViewController will call prepareForSegueafterawakeFromNib

it's called up viewDidLoad

so you can set the delegate to

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

First

EmbededSegue

enter image description here

embededVC prepareForSegue

enter image description here

, EmbededViewController MainViewController enter image description here

+1

All Articles