如何从NSDictionary分配图像和标签值?

时间:2015-10-29 22:15:26

标签: ios swift

我将数据(图像和标签)从一个视图控制器传递到另一个视图控制器。我将它们存储在NSDictionary中,并将数据传递给新的viewcontroller。我已连接图像视图和标签。如何为NSDictionary中的值分配图像和标签?下面是将数据从一个视图控制器传输到另一个视图控制器的代码。

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的代码。

@IBOutlet weak var friendimage: UIImageView!
@IBOutlet weak var friendname: UILabel!
var dicData : NSDictionary?

override func viewWillAppear(animated: Bool) {
print("Dictionary: \(self.dicData)")

}

2 个答案:

答案 0 :(得分:2)

或者您可以创建具有属性

的模型类
var image: UIImage?
var text: String?

然后将该类的对象传递给下一个视图控制器,并将这些值分配给friendimage.imagefriendname.text

FriendProfilePages代码:

@IBOutlet weak var friendimage: UIImageView!
@IBOutlet weak var friendname: UILabel!
var dataModel : DataModel?

override func viewWillAppear(animated: Bool) {
   super.viewWillAppear(animated)
   friendimage.image = dataModel.image
   friendname.text = dataModel.text
}

现在执行Segue

var selectedData = DataModel()

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
   print("Cell \(indexPath.item) selected")
   print(arrayOfFriendsNames[indexPath.item])

   self.selectedData.text = arrayOfFriendsNames[index] as? String
   self.selectedData.image = arrayOfFriends[index] as? UIImage
   self.performSegueWithIdentifier("friendaccess", sender: dicSelected)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if(segue.identifier == "friendaccess"){
    let nextViewOBJ = segue.destinationViewController as! FriendProfilePages
    nextViewOBJ.dataModel = self.selectedData;
   }
}

在任何地方声明

class DataModel : NSObject {
   var image: UIImage?
   var text: String?
}

只是为了让你有个主意...

答案 1 :(得分:0)

您应该使用正确的类型来传输数据而不是字典。你可以定义一个合适的类型,如

template <typename T, std::size_t M, std::size_t N>
inline
void my_fn( const T (&a)[M][N] )
{
  return my_fn( (const T*)a, M, N );
}

您需要更新您的选择代码以创建这样的新结构之一

int main()
{
  int xs[5][3];
  my_fn( xs );

  double ys[12][7];
  my_fn( ys );

您的struct FriendProfileData { let name: String let image: UIImage } 需要更改为

var selected: FriendProfileData

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  selected = FriendProfileData(name: arrayOfFriendsNames[indexPath.item], image: arrayOfFriends[indexPath.item])
  performSegueWithIdentifier("friendaccess", sender: self)
}

然后在viewDidAppear

prepareForSegue