[英]How do I send data from selectedrow to next viewcontroller?
I have a collectionview with cells containing username and image retrieved from parse. 我有一个collectionview,其中包含从解析中检索到的用户名和图像的单元格。 I have a view controller with a blank image and blank label, when the cell is clicked I want the user label and image from the collectionview to be placed into the blank image and label on the view controller.
我有一个带有空白图像和空白标签的视图控制器,当单击单元格时,我希望将用户标签和来自collectionview的图像放入视图控制器上的空白图像和标签中。 I know I have to use
prepareForSegue
, but I am not sure what to do after. 我知道我必须使用
prepareForSegue
,但是我不确定之后该怎么做。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView
cell.friendname.text = arrayOfFriendsNames[indexPath.row]
cell.friendpic.image = arrayOfFriends[indexPath.row]
//cell.friendpic.image = UIImage(named: arrayOfFriendsTest[indexPath.row])
cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
cell.friendpic.clipsToBounds = true
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Cell \(indexPath.row) selected")
print(arrayOfFriendsNames[indexPath.row])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "friendaccess"){
}
}
class FriendProfile: UIViewController {
var friendname = UILabel()
var friendimage = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
friendname = UILabel()
friendname = UILabel(frame: CGRectMake(0,0,200,60))
friendname.frame.origin.y = userpic.frame.size.height + 10.0
friendname.frame.origin.x = (self.view.bounds.size.width - userpicname.frame.size.width) / 2.0
friendname.textAlignment = NSTextAlignment.Center
friendname.text = "Username"
friendname.font = UIFont(name: "Lombok", size: 60)
friendname.textColor = UIColorFromRGB("4A90E2")
self.view.addSubview(friendname)
friendimage = UIImageView(image: UIImage(named: "default.png")!)
friendimage.frame = CGRectMake(0, 0, 40, 20)
friendimage.frame.origin.x = (self.view.bounds.size.width - friendimage.frame.size.width)/2.0
friendimage.frame.origin.y = (self.view.bounds.size.height - 60)
self.view.addSubview(friendimage)
// Do any additional setup after loading the view.
}
This is the code for the scrollview 这是scrollview的代码
override func viewDidLoad() {
super.viewDidLoad()
let FriendProfileView: FriendProfile = self.storyboard?.instantiateViewControllerWithIdentifier("FriendsProfile") as! FriendProfile
let FriendMusicView: FriendsMusic = self.storyboard?.instantiateViewControllerWithIdentifier("FriendMusic") as! FriendsMusic
self.addChildViewController(FriendProfileView)
self.scrollView.addSubview(FriendProfileView.view)
FriendProfileView.didMoveToParentViewController(self)
self.addChildViewController(FriendMusicView)
self.scrollView.addSubview(FriendMusicView.view)
FriendMusicView.didMoveToParentViewController(self)
var FriendMusicFrame: CGRect = FriendProfileView.view.frame
FriendMusicFrame.origin.y = self.view.frame.height
FriendMusicView.view.frame = FriendMusicFrame
self.scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.size.height*2)
// Do any additional setup after loading the view.
}
enter code here
The information is contained in sender: 该信息包含在发件人中:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "friendaccess"){
(segue.destinationViewController as! MyViewController).friendimage.image = (sender as! FriendcellView).friendpic.image
(segue.destinationViewController as! MyViewController).friendname.text = (sender as! FriendcellView).friendName.text
}
}
Substituting MyViewController for your real class and the property names as accurate. 将MyViewController替换为您的真实类和属性名称(准确无误)。
This is dependant upon sender actually being your Cell Class, because it sends the segue. 这取决于发送方实际上是您的Cell Class,因为它发送segue。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Cell \(indexPath.row) selected")
print(arrayOfFriendsNames[indexPath.row])
let dictData = ["name" : arrayOfFriendsNames[indexPath.row], "image" : arrayOfFriends[indexPath.row]]
self.performSegueWithIdentifier("friendaccess", sender: dictData)
} }
and in: 并在:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "friendaccess"){
let controller = segue.destinationViewController as! MyViewController
let data = sender as NSDictionary
//Assign data to controller here you must create dicdata in destination controller
controller.data = data
}
} }
Write following code
in CollectionviewController class
. 在
CollectionviewController class
编写以下code
。
Take global object of NSDictionary
. 拿
global object of NSDictionary
。
var dicSelected : NSDictionary!
Then Write following code
然后写下面的
code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "PushNextViewController"){
var nextViewOBJ = segue.destinationViewController as! NextViewController
nextViewOBJ.dicData = self.dicSelected;
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.dicSelected = ["friendName" : arrFriends[indexPath.row], "FriendImage" : arrFriends[indexPath.row]]
self.performSegueWithIdentifier("PushNextViewController", sender: nil)
}
And in nextController
need to take one NSDictionary
as global object
and your code
looks like below. 在
nextController
需要使用一个NSDictionary
作为global object
,您的code
如下所示。
class NextViewController: UIViewController {
var dicData : NSDictionary!
override func viewDidLoad() {
super.viewDidLoad()
println(self.dicData);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.