简体   繁体   English

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

[英]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.

相关问题 我如何不断地将数据从父ViewController发送到ContainerViewController - How do I constantly send data from parent ViewController to ContainerViewController 如何在Swift 2.0中使用prepareForSegue将数据从PFTableViewCell传递到下一个ViewController? - How do I pass the data from PFTableViewCell to the next ViewController using prepareForSegue in swift 2.0? 如何将 self 作为委托从 tabViewController 中的一个 viewController 发送到另一个 viewController - How do I send self as a delegate from one viewController in a tabViewController to another viewController 如何在子类pickerview的viewcontroller中实现selectedrow - how to implement selectedrow in viewcontroller for subclassed pickerview 如何将字符串发送到另一个ViewController - How do I send strings to another viewcontroller swift 5中如何从viewcontroller发送数据到embedded viewcontroller? - How to send data from viewcontroller to embded viewcontroller in swift 5? 如何使用从ViewController2传回的数据更新ViewController1数据 - How do I update ViewController1 data with data passed back from ViewController2 如何将数据从ViewController传递到Container View? - How do I pass data from my ViewController to a Container View? 如何将数据从 viewController 传递到 swiftUI 视图? - How do I pass data from viewController to swiftUI view? 如何使“全部查看”按钮通过segue到下一个ViewController显示数据? - How do I make a “View All” button display data via segue to the next ViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM