salipshitz
salipshitz

Reputation: 67

Swift 4- App crashes on button press that starts segue

I am programming an application and it is supposed to go to another view controller on a button press but instead it crashes when I run it. The editor shows this: Thread 1: signal SIGABRT and with editor breakpointing on it shows this: Thread 1: breakpoint 1.1 but there is no error in the console. Here is the code for the viewcontroller that does the segue:

// 
//  DoTodayViewController.swift
//  Paley Foundation: Starfish Support
//
//  Created by Sa'ar Lipshitz on 10/28/17.
//

import UIKit

class DoTodayViewController: UIViewController {
    @IBOutlet weak var navItem: UINavigationItem!
    @IBOutlet weak var getBtn: UIButton!
    @IBOutlet weak var suggestBtn: UIButton!
    var name = "Sa'ar"

    override func viewDidLoad() {
        super.viewDidLoad()
        getBtn.layer.cornerRadius = 8
        getBtn.clipsToBounds = true
        suggestBtn.layer.cornerRadius = 8
        suggestBtn.clipsToBounds = true
        navItem.prompt = "What would you like to do today, "+name+"?"
    }

    @IBAction func suggest(_ sender: Any) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "SuggestViewController") as! SuggestViewController
        navigationController?.pushViewController(vc, animated: true)
    }
    //This crashes my app:
    @IBAction func get(_ sender: Any) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "AccessViewController") as! AccessViewController
        navigationController?.pushViewController(vc, animated: true)
    }
}

and here is my code for the viewcontroller that gets opened:

//
//  AccessViewController.swift
//  Paley Foundation: Starfish Support
//
//  Created by Sa'ar Lipshitz on 10/29/17.
//

import UIKit
import Firebase
import Material

class AccessViewController: UIViewController {
    var adviceRef: DatabaseReference!
    var advice: [[String: Any]] = []
    var i = 0
    var card: Card!
    var loadingLbl: UILabel!
    var panGesture = UIPanGestureRecognizer()
    var cardOrigPos: CGPoint!

    override func viewDidLoad() {
        super.viewDidLoad()
        adviceRef = Database.database().reference().child("Advice")
        var advDict: NSDictionary?
        adviceRef.observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary
            advDict = value
            print(advDict as Any)
            for (_, adv) in advDict! {
                self.advice.append(adv as! [String : Any])
            }
            print(self.advice)
            self.showNextCard()
        }) { (error) in
            print(error.localizedDescription)
        }
        let swipeRec = UIPanGestureRecognizer(target: self, action: #selector(swipe(sender:)))
        card.isUserInteractionEnabled = true
        card.addGestureRecognizer(swipeRec)
    }

    @objc func swipe(sender: UIPanGestureRecognizer) {
        let translation = sender.translation(in: view)
        let velocity = sender.velocity(in: view)
        let direction = velocity
        print("Translation: \(translation)")
        if sender.state == .began {
            cardOrigPos = card.center
        } else if sender.state == .changed {
            card.center = CGPoint(x: cardOrigPos.x+translation.x, y: cardOrigPos.y+translation.y)
        } else if sender.state == .ended {
            if velocity.y == 0 && velocity.x == 0 {
                UIView.animate(withDuration: 0.3, animations: {
                    self.card.center = self.view.center
                })
            } else {
                UIView.animate(withDuration: 0.3, animations: {
                    self.card.center = CGPoint(x: self.cardOrigPos.x+direction.x*5, y: self.cardOrigPos.y+direction.y)
                })
                self.showNextCard()
            }
        }
    }

    func showNextCard() {
        if advice.count >= 0 {
            let adv = advice[i]
            setCard(adv["username"] as? String, adv["title"] as! String, adv["desc"] as! String, image: adv["image"] as? UIImage)
            i += 1
            if i >= advice.count {
                i = 0
            }
        }
    }

    func setCard(_ username: String?, _ title: String, _ desc: String, image: UIImage?) {
        let toolbar = setToolbar(username ?? "", title)
        card = Card()
        card.toolbar = toolbar
        card.toolbarEdgeInsetsPreset = .square3
        card.toolbarEdgeInsets.bottom = 0
        card.toolbarEdgeInsets.right = 8
        card.contentView = setContentView(content: desc)
        card.contentViewEdgeInsetsPreset = .wideRectangle3
        card.layer.borderWidth = 1.0
        card.layer.borderColor = UIColor.black.cgColor
        card.layer.cornerRadius = 2
        view.layout(card).horizontally(left: 20, right: 20).center()
    }

    func setToolbar(_ username: String, _ title: String) -> Toolbar {
        let toolbar = Toolbar(rightViews: [setMoreBtn()])
        toolbar.title = title
        toolbar.detail = username
        toolbar.detailLabel.textColor = Color.grey.base
        return toolbar
    }
    func setMoreBtn() -> IconButton {
        let moreBtn = IconButton(image: Icon.cm.moreVertical, tintColor: Color.grey.base)
        return moreBtn
    }
    func setContentView(content: String) -> UILabel {
        let contentView = UILabel()
        contentView.numberOfLines = 0
        contentView.text = content
        contentView.font = RobotoFont.regular(with: 14)
        return contentView
    }
}

When I tried using print statements to debug, the one in the DoTodayViewController.get(_ sender: Any) method ran but not the one in AccessViewController.viewDidLoad()

Upvotes: 0

Views: 677

Answers (1)

Santhosh R
Santhosh R

Reputation: 1568

Make sure you changed the class of the view controller object in the storyboard to AccessViewController and that the Storyboard ID is correctly spelled. You may be encountering a nil when trying to force unwrap storyboard?.instantiateViewController(withIdentifier: "AccessViewController") as! AccessViewController. Try using if let wherever an optional needs to be unwrapped to handle the scenario of it being nil.

Upvotes: 2

Related Questions