I'm trying to pass a string to my Modal view controller seen below, by using the prepareForSegue method. See Below:
Here is my Initial View Controller where i will present the modal view:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "newProject" {
var newProjectVC:ModalViewController = ModalViewController()
newProjectVC = segue.destinationViewController as ModalViewController
newProjectVC.testString = "hello"
}
}
Here is my modal view controller:
import UIKit
class ModalViewController: UIViewController {
var testString:NSString!
override func viewDidLoad() {
println(self.testString)
}
}
Here is what it looks like in storyboard:
The problem is that it throws an exception on this line:
newProjectVC = segue.destinationViewController as ModalViewController
I have a feeling it could be something to do with the navigation controller but am unsure, any ideas?
You are right the destinationViewController is navigation controller not ModalViewController, try that:
let navigationController = segue.destinationViewController as UINavigationController
let newProjectVC = navigationController.topViewController as ModalViewController
newProjectVC.testString = "hello"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With