0
votes

I have 2 view controller classes. I want to call a method of ViewController2 from ViewController, but the console gives me this error:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Here is my method in the ViewController class:

class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    @IBAction func testButton(sender: AnyObject) {
        ViewController2().setText("It's a Test!")
    }
}

And here is some code from ViewController2:

class ViewController2: UIViewController {

    @IBOutlet weak var directionsTextField: UITextView!


    func setText(var fieldText:String){
            directionsTextField.text = directionsTextField.text + "\n \n" + fieldText
    }
}

I'd be glad for every help! :-)

Thank you in advance :)

EDIT:

I also get this:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

on the setText method... Seems like directionsTextField isn't yet there maybe(??)

2
Why not use the delegation pattern? Is exactly for what you want to achieve here is the UIViewControllers are related (segue, etc) - Victor Sigler
How do I do that? I don't know how to use a delegation pattern or maybe I just don't exactly know what you mean. - unixb0y

2 Answers

2
votes

You're pretty close to figuring it out yourself. The crash is because your directionsTextField does not yet exist. Being an IBOutlet it won't come into existence before viewDidLoad is called. How do you fix this?

You change your API for your SecondViewControllerand give it a directionsText property. Winding up with something like this:

@IBOutlet weak var directionsTextField: UITextView!
var directionsText = ""

override func viewDidLoad() {
    super.viewDidLoad()
    directionsTextField.text = directionsTextField.text + "\n\n\(directionsText)"
}

In your FirstViewController you change your IBAction to match the new API of the SecondViewController:

@IBAction func testButton(sender: AnyObject) {
    let secondController = SecondViewController() // Felt a bit odd creating an instance without having a way of referencing it afterwards.
    secondController.directionsText = "It's a test"
    // Do what you want. (Perhaps pushing your secondController)
}
1
votes

The error occurs when you try instance the Second ViewController. Because you need implement the init method.

For example:

public class FirstViewController : UIViewController {

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

    let secondviewController = SecondViewController().setText("YOURTEXT")

}

}

The Second ViewController:

public class SecondtViewController: UIViewController {

public required init(coder aDecoder : NSCoder) {
    super.init(coder : aDecoder)
}

public init(nibName nibNameOrNil : String){
    super.init(nibName : nibNameOrNil, bundle: SBTVPayViewController.frameworkBundle())
}

public convenience init() {
    self.init(nibName: "MyNibName")
}

public func setText(texto : String) {

    directionsTextField.text = directionsTextField.text + "\n \n" + fieldText


}

}

When you try to instantiate a ViewController the first method to execute it's init