简体   繁体   中英

How do I send data from selectedrow to next viewcontroller?

I have a collectionview with cells containing username and image retrieved from parse. 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. I know I have to use prepareForSegue , but I am not sure what to do after.

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

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.

This is dependant upon sender actually being your Cell Class, because it sends the 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 .

Take global object of NSDictionary .

var dicSelected : NSDictionary!

Then Write following 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.

class NextViewController: UIViewController {

     var dicData : NSDictionary!

    override func viewDidLoad() {
        super.viewDidLoad()

        println(self.dicData);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

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