Sorry if this particular problem has been asked about, I followed the answers on other threads but none of them seemed to work, but I just started learning Swift so all of this is pretty new to me.
So, I have a text field in two View Controllers and I want the third View Control to display a result based on the input from the other two controllers when I press a button.
I followed this tutorial and placed the text fields, label and button like I said before. I placed my code (which you can see below) inside ViewControl.swift.
The problem is that when I attempt to run it I get a "Thread 1 :EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" error in the last two lines.
class ViewController: UIViewController {
var a: String = ""
var b: String = ""
@IBOutlet weak var aTextField: UITextField!
@IBOutlet weak var bTextField: UITextField!
@IBOutlet weak var calculateButton: UIButton!
@IBOutlet weak var resultLabel: UILabel!
@IBAction func calculateButtonPressed(_ sender: UIButton) {
let a = aTextField.text!;
let b = bTextField.text!;
I think that the error is from the data not passing between the views (because before I had everything in the same view and it worked fine), but since I only have one ViewController.swift file I couldn't figure out how to use a Segue.
Do not declare same variables multiple times. Remove let
before a
& b
. You have already declared a
& b
globally and then tried to redeclare it inside IBAction
class ViewController: UIViewController {
var a: String = ""
var b: String = ""
@IBOutlet weak var aTextField: UITextField!
@IBOutlet weak var bTextField: UITextField!
@IBOutlet weak var calculateButton: UIButton!
@IBOutlet weak var resultLabel: UILabel!
@IBAction func calculateButtonPressed(_ sender: UIButton) {
a = aTextField.text!;
b = bTextField.text!;
@IBAction func calculateButtonPressed(_ sender: UIButton) { self.a = aTextField.text! self.b = bTextField.text! }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.