swiftarraylistside-menu

Cannot get anything out of the pages from the side menu


I am building up on this (https://www.youtube.com/watch?v=e8OtfA3YvSM) awesome tutorial on side menus with cocoa pods. I managed to throw some objects on the home page (like a scroll view and a page control) and they show up perfectly. However, I cannot seem to get anything done right on the pages (Cocoa Touch Class) that are linked to the side menu. For instance, I cannot even add a dumb picture on the ‘Link 1’ page, even though I follow simple and straightforward procedures: (1) I create a storyboard view controller; (2) I assign it to the correct class (which in my example is Link1); (3) I add a UIImageView to the view controller with all the constraints and everything.

Now if I simply select an image on the storyboard it wouldn’t show up when I run the app (but for some reason I thought that simply adding a picture would suffice).

(4) I delete the image out of the image view; (5) I connect the image view with the Link1 view controller through @IBOutlet. (6) Then, in view did load I add this:

imageView.image = UIImage(named: "applespicture")

and it crashes… and I’m sitting there like an idiot.

I know this is probably a dumb question and an easy fix, but I am far at the beginning of this endless journey. So could you please tell me what am I doing wrong? And, more importantly, how should I go about swift files Link1, Link2 and Link3. Is there a specific way how I should code there or add anything there because nothing seems to show on these pages, no matter what I do. And in my project, I have like 15 of them so I need to get a hold of it now.

Here’s the code for my side menu, which is on the home page:

import UIKit
import SideMenu



class HomeViewController: UIViewController, MenuControllerDelegate {
private var sideMenu: SideMenuNavigationController?
private let link1Controller = Link1()
private let link2Controller = Link2()
private let link3Controller = Link3()



override func viewDidLoad() {
    super.viewDidLoad()
    let menu = MenuController(with: SideMenuItem.allCases)
    menu.delegate = self
    sideMenu = SideMenuNavigationController(rootViewController: menu)
    sideMenu?.leftSide = true
    SideMenuManager.default.leftMenuNavigationController = sideMenu
    SideMenuManager.default.addPanGestureToPresent(toView:  view)
    addChildControllers()
}


private func addChildControllers() {
    addChild(link1Controller)
    addChild(link2Controller)
    addChild(link3Controller)
    
    view.addSubview(link1Controller.view)
    view.addSubview(link2Controller.view)
    view.addSubview(link3Controller.view)
    
    link1Controller.view.frame = view.bounds
    link2Controller.view.frame = view.bounds
    link3Controller.view.frame = view.bounds
    
    link1Controller.didMove(toParent: self)
    link2Controller.didMove(toParent: self)
    link3Controller.didMove(toParent: self)
    
    link1Controller.view.isHidden = true
    link2Controller.view.isHidden = true
    link3Controller.view.isHidden = true
}



@IBAction func didTap( sender: Any) {
    present(sideMenu!, animated: true)
}



func didSelectMenuItem(named: SideMenuItem) {
    sideMenu?.dismiss(animated: true, completion: nil)
    title = named.rawValue
    switch named {
    case .home:
        link1Controller.view.isHidden = true
        link2Controller.view.isHidden = true
        link3Controller.view.isHidden = true
    case .link1:
        link1Controller.view.isHidden = false
        link2Controller.view.isHidden = true
        link3Controller.view.isHidden = true
    case .link2:
        link1Controller.view.isHidden = true
        link2Controller.view.isHidden = false
        link3Controller.view.isHidden = true
    case .link3:
        link1Controller.view.isHidden = true
        link2Controller.view.isHidden = true
        link3Controller.view.isHidden = false } }
}



protocol MenuControllerDelegate {
func didSelectMenuItem(named: SideMenuItem) }



enum SideMenuItem: String, CaseIterable {
case home = "Home"
case link1 = "Link 1"
case link2 = "Link 2"
case link3 = "Link 3" }



class MenuController: UITableViewController {



public var delegate: MenuControllerDelegate?
private let menuItems: [SideMenuItem]
private let tiffanyColor = UIColor(
    red: 129/255,
    green: 216/255,
    blue: 208/255,
    alpha: 1)



init(with menuItems: [SideMenuItem]) {
    self.menuItems = menuItems
    super.init(nibName: nil, bundle: nil)
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") }



required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented") }



override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundColor = tiffanyColor }



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    navigationController?.isNavigationBarHidden = true
    return menuItems.count }



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = menuItems[indexPath.row].rawValue
    cell.textLabel?.textColor = .white
    cell.backgroundColor = tiffanyColor
    return cell }



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let selectedItem = menuItems[indexPath.row]
    delegate?.didSelectMenuItem(named: selectedItem) }
}

And also what I have for the Link1 page:

import UIKit

class Link1: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    imageView.image = UIImage(named: "applespicture")
    
    }
}

Oh, and when it crashes, it says "Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value on line 18". This is where I have

imageView.image = UIImage(named: "applespicture")

But I am afraid even if I fix this issue, the Link1-3 pages will still show no content.

Btw, the only thing I can do there is view.backgroundcolor =)


Solution

  • I looked at project you sent me, found issues:

    popary.image = UIImage(named: "applespicture")
    
    private let link1Controller = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "link1ViewController")