0

how do you pass data from search Bar to UILabel in a different view controller

Passing Data between View Controllers I have tried multiple questions here on stack, but it just doesn't work or the simulator ends up crashing for me

class FirstVC: UIViewController, DataEnteredDelegate {

    @IBOutlet weak var label: UILabel!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showSecondViewController" {
            let secondViewController = segue.destination as! SecondVC
            secondViewController.delegate = self
        }
    }

    func userDidEnterInformation(info: String) {
        label.text = info
    }
}



 // protocol used for sending data back

protocol DataEnteredDelegate: class {
    func userDidEnterInformation(info: String)
}

class SecondVC: UIViewController {

    // making this a weak variable so that it won't create a strong reference cycle
    weak var delegate: DataEnteredDelegate? = nil

    @IBOutlet weak var searchBar: UISearchBar!

    @IBAction func sendTextBackButton(sender: AnyObject) {

        // call this method on whichever class implements our delegate protocol
        delegate?.userDidEnterInformation(info: searchBar.text!)

        // go back to the previous view controller
        _ = self.navigationController?.popViewController(animated: true)
    }
}

2 Answers2

0

When I need pass just small values between 2 viewControllers, I don't use a delegate, just create a variable in second view and pass like this.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "showView") {
        let vc = segue.destination as! secondViewController
        vc.stringTemp = "My string"
    }
}
Humberto Lima
  • 46
  • 1
  • 3
0

I don't find anything suspicious in your code which cause app crash. Meanwhile for data transfer between view controller we can use delegation and NotificationCenter. Below code uses NotificationCenter

let kStringTransfer = "StringTransferNotification"

class FirstViewController: UIViewController, DataEnteredDelegate {

    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(setString(notify:)), name: NSNotification.Name(rawValue: kStringTransfer), object: nil)
    }

    func getSecondVC() {
        if let second = self.storyboard?.instantiateViewController(withIdentifier: "Second") as? ViewController {
            self.navigationController?.pushViewController(second, animated: true)
        }
    }

    @IBAction func searchBarBtn(_ sender: Any) {
        //go to search view controller
        getSecondVC()
    }

    @objc func setString(notify: Notification) {
        //set search bar string to text field
        textField.text = notify.object as? String
    }

}

class SecondViewController: UIViewController, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        searchBar.delegate = self
    }

    @IBAction func goBackBtn(_ sender: Any) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kStringTransfer), object: searchBar.text)
        self.navigationController?.popViewController(animated: true)
    }
}

Amrit
  • 301
  • 2
  • 14