使用pushViewController传递数据?

时间:2017-01-23 10:32:24

标签: ios swift uiviewcontroller segue

使用此代码,我可以'segue'到我的视图控制器的同一个实例

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC")
    self.navigationController?.pushViewController(vc, animated: true)


}

但是,如何传递数据?我只知道如何使用segue选项传递数据。当我用这个运行代码时,我得到了nil错误,因为新的实例化视图控制器无法读取数据。

5 个答案:

答案 0 :(得分:9)

例如我在这里添加,有关详细说明,您可以从here

获取教程
class SecondViewController: UIViewController {

var myStringValue:String?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // We will simply print out the value here
    print("The value of myStringValue is: \(myStringValue!)")
}

并将字符串发送为

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! SecondViewController
      vc.myStringValue = "yourvalue"

    self.navigationController?.pushViewController(vc, animated: true)


}

答案 1 :(得分:5)

首先关闭。这不是segue。这只是将另一个视图推向堆栈。并且(如Ashley Mills所说)这与您目前所处的实例不同。这是一个视图控制器的新实例。

但您需要做的就是填充数据。你已经有控制器......

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    // you need to cast this next line to the type of VC.
    let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC // or whatever it is
    // vc is the controller. Just put the properties in it.
    vc.thePropertyYouWantToSet = theValue

    self.navigationController?.pushViewController(vc, animated: true)
}

答案 2 :(得分:4)

你所使用的并不是一个障碍。这只是将视图控制器的一个新实例(不是同一个)推送到导航堆栈。

要设置故事板,您可以在集合视图单元格中拖动链接到视图控制器,然后使用prepareForSegue方法分配数据......

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let viewController = segue.destinationViewController as? DetailVC {
        viewController.someProperty = self.someProperty
    }
}

答案 3 :(得分:2)

DetailVC中,创建变量并在创建object时指定值。检查以下示例:

class DetailVC {
 var dataString: String?
}

传递如下数据:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
    vc.dataString = "PASS THE DATA LIKE THIS"
    self.navigationController?.pushViewController(vc, animated: true)


}

答案 4 :(得分:1)

如果您遵循的是无故事板模式,则可以这样做

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let viewController = NextViewController()
    viewController.dataInsideNextViewController = "Data to be passed"
    navigationController?.pushViewController(viewController, animated: true)
}