Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Russell Jowell
Russell Jowell
4,261 Points

I need help setting up a custom protocol and delegate

Hi, I'm working on an app that shows government information. One part is a "Representative Finder" in which someone can enter their address, press a button, and see who represents them. The base of this is a VC with a table that lists the results, and the search box is in an embedded container view. I'm having trouble setting up the delegate/protocol to transfer the data from the embedded TextField to the UITable in the root VC. My code is as follows:

SEARCH VC: ``` import UIKit

      protocol LocalSearchInputDelegate: class
      {
          func showString(data: String)
     }

      class LocalSearchWindow: UIViewController {




weak var textDelegate: LocalSearchInputDelegate?

@IBOutlet weak var localSearchText: UITextField!

@IBOutlet weak var localSearchButton: UIButton!

func buttonPressed(sender: UIButton)
{
    print("spot1")
    textDelegate?.showString(data: "ooh rah")

}


override func viewDidLoad() {
    super.viewDidLoad()
    //textDelegate=currentVC as! LocalSearchInputDelegate
    localSearchButton.addTarget(self, action: #selector(buttonPressed), for: UIControlEvents.touchDown)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}```

TABLE CONTROLLER:

         ```class RepresentativeFinder: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate,    UITableViewDataSource, LocalSearchInputDelegate {

          func showString(data: String) {
          //These commands don't work       
         print("your data is")
               print(data)
              }```