Jeppe C Jeppe C - 2 months ago 15
Swift Question

@IBAction to performSegue

I have the following:

class Settings: UIViewController {

@IBAction func CitySend(sender: AnyObject) {
self.performSegue(withIdentifier: "senddata", sender: self)
}

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "senddata" {
let Mainview = segue.destination as! ViewController
Mainview.label = textField.text!
}
}
}


My mainview looks like this:

class ViewController: UIViewController {

@IBOutlet var city: UILabel!
var label: String = ""


override func viewDidLoad() {
super.viewDidLoad()


city.text = label
}

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


im not sure, if this is how i should even do this, but to me the logic is that i only want to allow this segue to pass a string to my label in my mainview if the button is pressed (Like a save button)

However as to what i have made now, nothing happens, and it gives me the 1: signal SIGABRT error, whenever i press the button.

Answer

Use the following code:

ViewController.swift

 import UIKit

class ViewController: UIViewController, SecondViewControllerProtocol{
    @IBOutlet weak var dataLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    @IBAction func navigateToSecondViewControllerOnButtonClick(_ sender: AnyObject) {

        let  secondVC: ViewController2 = storyboard?.instantiateViewController(withIdentifier: "viewcont2") as! ViewController2
        secondVC.delegate = self
        self.navigationController?.pushViewController(secondVC, animated: true)
    }

    func saveData(data: String){
        dataLabel.text = data
    }

}

ViewController2.swift

 import UIKit


protocol SecondViewControllerProtocol {
    func saveData(data: String) // this function the first controllers
}

class ViewController2: UIViewController {

    var delegate: SecondViewControllerProtocol?

    @IBOutlet weak var dataTextField: UITextField!

    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.
    }

    @IBAction func saveButtonClicked(_ sender: AnyObject) {
        delegate?.saveData(data: dataTextField.text!)
    }
}

Storyboard screenshot:

enter image description here

Please check my GitHub link below to test sample:

https://github.com/k-sathireddy/PassingDataThroughSegue