简体   繁体   中英

Passing data between two view controllers are not working

I am trying to pass some data between two view controllers, but it doesn't work..

This is the data i am trying to pass(these has items from parse.com - the same code is in both view controllers):

var userFile = [PFFile]()
var createdAt = [NSDate]()
var objID = [String]()

This is the button for open the view controller(inside the first view controller i am trying to send data FROM):

@IBAction func openButtonAction(sender: AnyObject) {
        let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
        modalVC.userFile = self.userFile
        modalVC.createdAt = self.createdAt
        modalVC.objID = self.objID

        print("USERFILE: \(modalVC.userFile.count)")
        presentViewController(modalVC, animated: true, completion: nil)
    }

The view controller is a ModalViewController.xib connected to ViewStoryModalViewController.swift

This is the viewDidLoad in the view controller i am trying to send data TO:

override func viewDidLoad() {
        super.viewDidLoad()

        print("USERFILECOUNT: \(self.userFile.count)")
    }

My problem is that this is the messages i get in xCode output: 在此处输入图片说明

What might be wrong here? Any suggestions?

xCode output tells that an array self.userFile contains zero elements, It doesn't mean that it is passed wrong. It is just empty.

print("USERFILECOUNT: \(self.userFile.count)")

Check if it is empty before passing it to modal vc.

Try this code You first need to present after that try to set variable.

IBAction func openButtonAction(sender: AnyObject) {
        let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
      print("USERFILE: \(modalVC.userFile.count)")
        presentViewController(modalVC, animated: true, completion: nil)
        modalVC.userFile = self.userFile
        modalVC.createdAt = self.createdAt
        modalVC.objID = self.objID
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM