Swift:以编程方式导航到 ViewController 并传递数据
Swift: Programmatically Navigate to ViewController and Pass Data
我最近开始学习swift,到目前为止还不错。目前我在尝试在视图控制器之间传递数据时遇到问题。我设法弄清楚如何使用导航控制器以编程方式在两个视图控制器之间导航。唯一的问题是现在我很难弄清楚如何将用户输入的三个字符串(json api)传递到下一个视图。
这是我目前的尝试。非常感谢任何帮助!
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
}
第二ViewController:
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.
}
}
方法 prepareForSegue
在您的应用的情节提要执行转场(您在情节提要中使用 Interface Builder 创建的连接)时调用。在上面的代码中,尽管您自己向控制器展示了 presentViewController
。在这种情况下,prepareForSegue
不会被触发。您可以在展示控制器之前进行设置:
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 的内容 here
更新了 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)
我最近开始学习swift,到目前为止还不错。目前我在尝试在视图控制器之间传递数据时遇到问题。我设法弄清楚如何使用导航控制器以编程方式在两个视图控制器之间导航。唯一的问题是现在我很难弄清楚如何将用户输入的三个字符串(json api)传递到下一个视图。
这是我目前的尝试。非常感谢任何帮助!
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
}
第二ViewController:
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.
}
}
方法 prepareForSegue
在您的应用的情节提要执行转场(您在情节提要中使用 Interface Builder 创建的连接)时调用。在上面的代码中,尽管您自己向控制器展示了 presentViewController
。在这种情况下,prepareForSegue
不会被触发。您可以在展示控制器之前进行设置:
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 的内容 here
更新了 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)