【问题标题】:Swift: Programmatically Navigate to ViewController and Pass DataSwift:以编程方式导航到 ViewController 并传递数据
【发布时间】:2015-10-27 03:11:52
【问题描述】:

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

这是我目前的尝试。非常感谢任何帮助!

视图控制器:

/* 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
}

第二视图控制器:

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

【问题讨论】:

    标签: ios swift uinavigationcontroller uistoryboardsegue


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

      更新了 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-20
        • 2019-07-02
        • 2017-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        相关资源
        最近更新 更多