如何将数据从selectedrow发送到下一个viewcontroller?

时间:2015-10-27 03:34:33

标签: ios swift

我有一个包含从解析中检索到的用户名和图像的单元格的集合视图。我有一个带有空白图像和空白标签的视图控制器,当单击单元格时,我希望将集合视图中的用户标签和图像放入视图控制器上的空白图像和标签中。我知道我必须使用viewDidAppear,但我不知道该怎么办。

prepareForSegue

这是scrollview的代码

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.
}

3 个答案:

答案 0 :(得分:1)

信息包含在发件人中:

 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
    }
}

将MyViewController替换为您的真实类,并将属性名称替换为准确。

这取决于发件人实际上是你的Cell Class,因为它发送了segue。

答案 1 :(得分:0)

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)

}

并在:

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
}

}

答案 2 :(得分:0)

code中写下CollectionviewController class

选择global object of NSDictionary

var dicSelected : NSDictionary!

然后写下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)
    }

nextController中需要将NSDictionary作为global objectcode如下所示。

class NextViewController: UIViewController {

     var dicData : NSDictionary!

    override func viewDidLoad() {
        super.viewDidLoad()

        println(self.dicData);
    }

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

}