Swift:以编程方式导航到ViewController并传递数据 [英] Swift: Programmatically Navigate to ViewController and Pass Data

查看:79
本文介绍了Swift:以编程方式导航到ViewController并传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习swift,到目前为止一直很好。目前我在尝试在视图控制器之间传递数据时遇到问题。我设法弄清楚如何使用导航控制器以编程方式在两个视图控制器之间导航。现在唯一的问题是我很难弄清楚如何将用户输入的三个字符串(对于json api)传递到下一个视图。

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:

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


推荐答案

当您的应用程序的故事板执行segue(您使用Interface Builder在故事板中创建的连接)时,将调用方法 prepareForSegue 。在上面的代码中,您将使用 presentViewController 自己呈现控制器。在这种情况下,不会触发 prepareForSegue 。您可以在呈现控制器之前立即进行设置:

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)

您可以阅读有关segue 这里

You can read more about segue here

这篇关于Swift:以编程方式导航到ViewController并传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆