Swift: Programmatically Navigate to ViewController

2020-07-22 16:52发布

问题:

I recently started to learn swift and it has been pretty good so far. Currently I'm having an issue trying to pass data between view controllers. I managed to figure out how to programmatically navigate between two view controllers using a navigation controller. Only problem is now I'm having a hard time trying to figure out how to pass three string entered by the user (for json api) to the next view.

Here's my current attempt. Any help is much appreciated!

ViewController:

/* Get the status code of the connection attempt */
func connection(connection:NSURLConnection, didReceiveResponse response: NSURLResponse){

    let status = (response as! NSHTTPURLResponse).statusCode
    //println("status code is \(status)")

    if(status == 200){

        var next = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
        self.presentViewController(next, animated: false, completion: nil)
    }
    else{

        RKDropdownAlert.title("Error", message:"Please enter valid credentials.", backgroundColor:UIColor.redColor(), textColor:UIColor.whiteColor(), time:3)
        drawErrorBorder(usernameField);
        usernameField.text = "";
        drawErrorBorder(passwordField);
        passwordField.text = "";
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    let navigationController = segue.destinationViewController as! UINavigationController
    let newProjectVC = navigationController.topViewController as! SecondViewController
    newProjectVC.ip = ipAddressField.text
    newProjectVC.username = usernameField.text
    newProjectVC.password = passwordField.text
}

SecondViewController:

import UIKit

class SecondViewController: UIViewController {

var ip:NSString!
var username:NSString!
var password:NSString!

override func viewDidLoad() {
    super.viewDidLoad()

    println("\(ip):\(username):\(password)")
}

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

回答1:

The method prepareForSegue is called when your app's storyboard performs a segue (a connection that you create in storyboards with Interface Builder). In the code above though you are presenting the controller yourself with presentViewController. In this case, prepareForSegue is not fired. You can do your setup right before presenting the controller:

let next = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
next.ip = ipAddressField.text
next.username = usernameField.text
next.password = passwordField.text
self.presentViewController(next, animated: false, completion: nil)

You can read more about segue here



回答2:

Updated syntax for Swift 3:

    let next = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController 
    next.ip = ipAddressField.text
    next.username = usernameField.text
    next.password = passwordField.text
    self.present(next, animated: true, completion: nil)