为什么在将数据从一个viewcontroller传递到另一个时,我收到一个nil?

时间:2015-10-29 02:02:28

标签: ios swift

我正在尝试将所选单元格的标签文本和图像传递给另一个 ViewController 。我正在使用NSDictionary,但在打印nil时收到NSDictionary。我正确地实例化了吗?为什么我收到nil值?我如何解决它?以下是我UICollectionView的代码。

var dicSelected : NSDictionary!
 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Cell \(indexPath.item) selected")
    print(arrayOfFriendsNames[indexPath.item])
     self.dicSelected = ["friendname" : arrayOfFriendsNames[indexPath.item], "friendimage" :  arrayOfFriends[indexPath.item]]
    self.performSegueWithIdentifier("friendaccess", sender: dicSelected)
}

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "friendaccess"){

        let nextViewOBJ = segue.destinationViewController as! FriendProfilePages

        nextViewOBJ.dicData = self.dicSelected;

    }
}

以下是 FriendProfilePages 的代码。

var friendname = UILabel()
var friendimage = UIImageView()
var dicData : NSDictionary?

 override func viewWillAppear(animated: Bool) {
      //receiving nil here
    print("Dictionary: \(self.dicData)")
}

2 个答案:

答案 0 :(得分:0)

在prepareForSegue的第一行设置断点,然后单步查看正在发生的事情。我的猜测是segue id不匹配。

答案 1 :(得分:0)

Try to print it from the "viewDidAppear". Probably nothing is "set" to the object before it is fully present in the view hierarchy