简体   繁体   English

iOs Swift Collection视图dismissView

[英]iOs Swift Collection View dismissView

I have a UIViewController where i need to pass information.For that i have made another UIViewController where i have a CollectionView. 我有一个需要传递信息的UIViewController,为此我做了另一个UIViewController,其中有CollectionView。

So when a user clicks on an element from the CollectionView it should close the view and go back to the main UIViewController. 因此,当用户单击CollectionView中的元素时,应关闭该视图并返回主UIViewController。

So i have managed to get it working with a button in the Navigaiton bar. 所以我设法通过Navigaiton栏中的按钮使其工作。

But when i place the same code 但是当我放置相同的代码

"dismissViewControllerAnimated(true, completion: nil)" “ dismissViewControllerAnimated(true,完成:nil)”

in the didSelectItemAtIndexPath its not working. 在didSelectItemAtIndexPath中不起作用。

I have tried to place a button in the cell but still same result everything works except that. 我试图在单元格中放置一个按钮,但除此以外,其他所有结果仍然相同。

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.exercisesNames.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellexercises_insert", forIndexPath: indexPath) as! Insert_ExerciseCollectionViewCell

    cell.insert_exerciseImage?.image = self.exercisesImages[indexPath.row]
    cell.insert_exerciseLabel?.text = self.exercisesNames[indexPath.row]
    cell.insert_exercisesHardnessImg?.image = self.exercisesHardnessImg[indexPath.row]

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     exercise_label="\(exercisesNames[indexPath.row])"
     exercise_image="\(exercises_Exervise_Row_Names[indexPath.row])"
     //self.dismissViewControllerAnimated(true, completion: nil)

     closeMe()
}




@IBAction func done(sender: AnyObject) {
    closeMe()
}

func closeMe(){
dismissViewControllerAnimated(true, completion: nil)
}

I am not sure because the context but I guess that you are trying to dismiss the collectionViewController but it wasn't presented modally so you can try: 我不确定这是因为上下文,但是我想您正在尝试关闭collectionViewController,但是它不是模态显示的,因此您可以尝试:

self.navigationController?.popViewControllerAnimated(true)

instead of dismissViewControllerAnimated(true, completion: nil) 而不是dismissViewControllerAnimated(true, completion: nil)

If in fact it was presented modally, make sure that your collectionViewController is embedded in a UINavigationController. 如果实际上是以模态显示的,请确保您的collectionViewController嵌入在UINavigationController中。

If it's presented modally you need to implement an unwind segue function in your first view controller. 如果以模态形式显示,则需要在第一个视图控制器中实现unwind segue功能。

@IBAction func unwindToMainViewController(segue: UIStoryboardSegue) {
}

Once you have done so go to the storyboard and control-drag from your view controller icon on the second view controller to the exit icon of your main view controller which you will be able to see on the bar to the left. 完成此操作后,转到情节提要,然后从第二个视图控制器上的视图控制器图标拖动到主视图控制器的退出图标,您将可以在左侧的栏中看到该图标。 It will then let you select your unwind segue. 然后,它将让您选择放松的时间。

Select your segue and set a segue identifier for it. 选择您的segue并为其设置segue标识符。

For your didSelectItemAtIndexPath function, you must call your segue. 对于didSelectItemAtIndexPath函数,必须调用segue。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("segueIdentifier", sender: self)
}

To send objects to your main view controller you can use the following function in your second view controller. 要将对象发送到主视图控制器,可以在第二个视图控制器中使用以下功能。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {       
    if(segue.identifier == "segueIdentifier") {
        let mainViewController = (segue.destinationViewController as mainViewController)
        mainViewController.classVariable = newVariableValue
    }
}

For more information please refer to: 有关更多信息,请参阅:

What are Unwind segues for and how do you use them? 什么是Unwind segues,您如何使用它们?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM