简体   繁体   English

Swift:以编程方式导航到ViewController并传递数据

[英]Swift: Programmatically Navigate to ViewController and Pass Data

I recently started to learn swift and it has been pretty good so far. 我最近开始学习swift,到目前为止一直很好。 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. 现在唯一的问题是我很难弄清楚如何将用户输入的三个字符串(对于json api)传递给下一个视图。

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: 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.
}
}

The method prepareForSegue is called when your app's storyboard performs a segue (a connection that you create in storyboards with Interface Builder). 当应用程序的故事板执行segue(使用Interface Builder在故事板中创建的连接)时,将调用prepareForSegue方法。 In the code above though you are presenting the controller yourself with presentViewController . 在上面的代码中,您将使用presentViewController自己呈现控制器。 In this case, prepareForSegue is not fired. 在这种情况下,不会触发prepareForSegue 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 你可以在这里阅读更多关于segue的内容

Updated syntax for Swift 3: 更新了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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当以编程方式创建的按钮快速单击时导航到 viewController - Navigate to viewController when programmatically created button click in swift iOS Swift通过推送通知以编程方式导航到某些ViewController - iOS Swift Navigate to certain ViewController programmatically from push notification 以一种编程方式将数据从一个viewcontroller传递到现有的viewcontroller - Pass data from one viewcontroller to existing viewcontroller programmatically 如何在Swift中在SplashScreen和ViewController之间传递数据 - How to pass data between SplashScreen and ViewController in Swift 如何在iOS swift中将数据传递给以前的viewController? - how to pass data to previous viewController in iOS swift? 在Swift中的TableView和另一个ViewController之间传递数据 - Pass data between TableView and another ViewController in Swift SWIFT:对TableView进行排序并将数据传递给第二个viewController - SWIFT: sorting TableView and pass data to second viewController iOS Swift:将数据从ViewController传递到UITableViewCell - iOS Swift: Pass data from ViewController to UITableViewCell 使用NSUserDefaults(Swift)在ViewController和TableVC之间传递数据 - Pass data between ViewController and TableVC with NSUserDefaults (Swift) Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM