49052

Dismiss the view controller which have navigation controller and pass the value back to main view co

Question:

I have an mainViewcontroller in that one button called get value.Then i am calling dataviewcontroller to select any item in collection view cell.Once user select any cell.That particular dataviewcontroller will dismiss and while dismiss it will have the user selected item name and it will display in mainViewcontroller .Now the view controller is not dismissing.

Here the code :

In my mainViewcontroller:

var SelectedName: String? = "" override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) currentTF.text = SelectedName }

Now dataviewcontroller :

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if collectionView.cellForItem(at: indexPath) != nil { selectedNamesss = allitems[indexPath.row] as String } calldismiss() print(selectedNamesss) } func calldismiss() { if let presenter = presentingViewController as? mainViewcontroller { presenter.SelectedName = selectedNamesss } dismiss(animated: true, completion: nil) }

Now how can i solve this.My viewcontroller is not dismising and values also not showing.

Thanks in advance ~

Answer1:

My Suggestion is to use "UnwindSegue" technique, which is simply works like "PerformSegue", So you can send back values very easily using prepare for segue and will be back to main controller. <a href="https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/" rel="nofollow">Here is tutorial for unwind segue</a>. Or you can find more from google.

Answer2:

For The problem where data is not showing, problem is with this condition:

if let presenter = presentingViewController as? mainViewcontroller { presenter.SelectedName = selectedNamesss }

this condition will never be true as the presentingViewController would always be a UINavigationController. so you need to change that may be something like follows:

if let presenter = (presentingViewController as! UINavigationController).viewControllers.first! as? mainViewcontroller { presenter.SelectedName = name }

Here i am using viewControllers.first! in condition as mainViewcontroller was at first index of the viewControllers array. Check the viewControllers array and find the index of mainViewcontroller and make changes accordingly.

I made this changes and the code is working for me. I am getting the selected name on the mainViewcontroller.

Also i would like to mention that this is not the right way to transfer data backwards. You can use delegates, blocks or unwind segue to achieve this.

Though this is working i am attaching a gif to show this working.

<a href="https://i.stack.imgur.com/rE9Yc.gif" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/rE9Yc.gif" data-original="https://i.stack.imgur.com/rE9Yc.gif" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Regarding the issue where your controller is not being dismissed, i am presenting the controller in following way:

let dataVC = self.storyboard?.instantiateViewController(withIdentifier: "dataviewcontroller") as! dataviewcontroller self.present(vc, animated: true, completion: nil)

Try this and see if this helps you :)

Recommend

  • async Task.Run with MVVM
  • Swift - Connect Delegate to Custom Xib Cell
  • Loading data into UICollectionView bunch by bunch
  • Set insets to the CollectionView programmatically in Swift
  • Changing the position of the autolayout enabled control revert the changes?
  • Can´t change UICollectionView Size of UICollectionViewController
  • rightAnchor constraint did not apply when added programmatically - swift
  • AVPlayer cannot play video
  • UICollectionviewCell Animations
  • UICollectionView not loading fully until I scroll
  • How to correctly animate change of size of a paged UICollectionView
  • CollectionView Highlights incorrect Cell When Scrolling
  • how to pass data between UICollectionViewLayout and UIViewController [duplicate]
  • UICollectionView inside CustomTableCell
  • How can I make my cells selectable when there is an UIView below the UICollectionView?
  • How to access buttons in a UICollectionView from a target function set (Swift 3)
  • UITableView in UICollectionView
  • UICollectionViewCell Only Updates After Scrolling
  • UICollectionview footer in swift
  • How to get the index path of a UICollectionView header?
  • Realm Object-Level Notifications inside UICollectionViewCell's
  • Design UIView subclass auto layout with custom size in Interface Builder xib file
  • CSStickyHeaderFlowLayout swift
  • Messing UIButton in each UICollectionViewCell in Scrolling Filmstrip
  • How to select CollectionView cell in RxSwift
  • How can I pass the value from a view to another view in iOS?
  • How to get label in cells in table to go on to next line instead of getting cut off screen? (xcode 8
  • UIRefreshControl with low height UICollectionView
  • How to enable UIDragInteraction on Custom UIViews for iPhone
  • Hilbert Transform (Analytical Signal) using Apple's Accelerate Framework?
  • Use two different cell identifier on same table view
  • Cells become empty after scrolling. (Xcode)
  • Delete UITable sections dynamically with commitEditingStyle in Swift
  • UItableviewcell background color while click
  • UITableView cell: selectedBackgroundView without showing backgroundView
  • Adjust UILabel Dynamically Within UITableViewCell?
  • Parse PFFile download order iOS
  • Auto Height of UICollectionView inside UITableViewCell
  • UITableView takes much longer to load when numberOfRows returns a large number
  • Adding a model to a Marionette CollectionView's collection doesn't trigger onItemAdd callb