0

I have:

  • a TableViewController with several users
  • a UICollectionViewController (a Chat)

I am trying to open the UICollectionViewController, when the User is clicking on one user in the TableViewController. The TableViewController should disappear, the UICollectionViewController (my Chat) should appear.

I am googling and stackoverflowing for 10 hours, but now im just desperated.

I have the following code in my TableViewController with all the users inside:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
IndexPath)
{
let destination = MainChatViewController()
present(destination, animated: true)
}

I am getting the error: Thread 1: signal SIGABRT error, and I have NO idea why. I appreaciate any help - thank you

George

JohnDole
  • 525
  • 5
  • 16
  • I think you need to use `self.storyboard?.instantiateViewController(withIdentifier: "Identifier") as! MainChatViewController` method instead of using `MainChatViewController()` – Rugmangathan Dec 01 '17 at 21:01
  • Have you set the collection view layout? – Paulo Dec 01 '17 at 21:02
  • Can you post the error message from your logs? – Rugmangathan Dec 01 '17 at 21:04
  • @Paulo: I am fairly new to SWIFT, if you mean something like addSubViews (in my function setupComponents()), then yes. – JohnDole Dec 01 '17 at 21:07
  • @Rugmangathan: nsinvalidArgumentException, reason: UICollectionView must be initialized with a non-nil layout parameter – JohnDole Dec 01 '17 at 21:08
  • Are you using storyboards to create your views, or are you doing it programmatically? – Paulo Dec 01 '17 at 21:08
  • @Paulo: Mainly storyboard, the UICollectionView programmatically! Thanks for your help – JohnDole Dec 01 '17 at 21:10
  • So, MainChatViewController is a UIViewController, with a collection view added? – Paulo Dec 01 '17 at 21:12
  • @Paulo. No. My MainChatViewController is a UICollectionViewController, in which I have Code to create a button and a textfield, constraints. I am using a function to create all items (e. g. let containerView = UIView(), view.addSubview(containerView) – JohnDole Dec 01 '17 at 21:15
  • Okay, so if you are using storyboards, I suggest you look at this post: https://stackoverflow.com/questions/43328838/how-to-perform-segue-to-different-controllers-in-uicollectionview – Paulo Dec 01 '17 at 21:18

2 Answers2

0

If you are definitely wanting to create your UICollectionViewController 100% programmatically like you show in your example you're going to have to program in all of the UI work, including implementing the loadView function.

I know some people are anti-Storyboard, but I feel you are creating unnecessary work when you could just lay out your viewController in storyboard, give it a Storyboard ID (for example "ChatViewController"), and in your tableView's didSelect method say:

let chatVC = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! MainChatViewController
self.present(chatVC, animated: true)
creeperspeak
  • 5,403
  • 1
  • 17
  • 38
  • I like this answer, for its simplicity and the users use of storyboards. Better than mine :) – Paulo Dec 01 '17 at 21:31
  • Thank you - just to clarify: If i use your solution, I get a "THREAD 1: EXC_BREAKPOINT (code=1, subcode=0x102*************) Error in my function which creates the textfield etc. Is this caused, because i create the objects programmicatally? – JohnDole Dec 01 '17 at 21:44
  • I have no way of knowing without seeing a lot more context. – creeperspeak Dec 01 '17 at 21:45
  • https://imgur.com/a/7HBIz Sorry for the quality. I gave my controller the correct storyboard ID! – JohnDole Dec 01 '17 at 22:10
  • That likely means that your optional tabBarController is in fact nil. You are crashing because you're force unwrapping it. You also don't need to constrain anything to the tabBarController - just constrain it to the viewController's `view` property and that'll be automatically adjusted to account for a tabBar. – creeperspeak Dec 01 '17 at 22:13
  • You are correct. When I exclude "containView.bottomAnchor.constraint(equalTo: view.BottomAnchor, constant: -(1) * (self.tabBarController?.tabBar.frame.size.height)!).isActive = true", it works! How can I access to my tabBar as well? Its missing, and i got it why, now the question is how to implement it as well as the collectionviewcontroller – JohnDole Dec 01 '17 at 22:29
  • Btw - when i dont constraint the tabBar height, my textfield will be on the bottom on my screen, hided by the tabBar – JohnDole Dec 01 '17 at 22:35
0

So, from what I can tell, you have a UICollectionViewController called MainChatViewController.

Your error message is occurring because a collection view requires a layout parameter when it is initialized. I wouldn’t say standard, but most use the flow layout

let layout = UICollectionViewFlowLayout()
let destination = MainChatViewController(collectionViewLayout: layout)
present(destination, animated: true, completion: nil)

Now this is not to say it is the only way of doing it, and I am sure that there are brighter people that can offer many other opinions.

Paulo
  • 602
  • 1
  • 8
  • 20