Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing NSString via prepareForSegue - Swift

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:

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?

like image 499
Ryan Avatar asked Aug 18 '14 15:08

Ryan


1 Answers

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"
like image 122
Greg Avatar answered Oct 07 '22 01:10

Greg