2017-11-10 115 views
0

我正在編程一個應用程序,它應該去一個按鈕按下另一個視圖控制器,但相反,它崩潰時,我運行它。編輯器顯示:Thread 1: signal SIGABRT並帶有編輯器突出顯示:Thread 1: breakpoint 1.1但在控制檯中沒有錯誤。 Here is the code for the viewcontroller that does the segueSwift 4-應用程序崩潰按鈕開始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) 
    } 
} 

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 
    } 
} 

當我試圖使用打印語句來調試,一個在DoTodayViewController.get(_ sender: Any)方法運行,但沒有一個在AccessViewController.viewDidLoad()

+2

您向我們展示了一個編輯器突破。爲什麼?什麼是錯誤?你怎麼知道應用程序崩潰? – Sulthan

回答

1

確保你改變了故事板中的視圖控制器對象的類別爲AccessViewController,並且Storyboard ID的拼寫正確。當您試圖強制解包storyboard?.instantiateViewController(withIdentifier: "AccessViewController") as! AccessViewController時,您可能會遇到零。無論需要展開一個可選項以處理它爲零的場景,請嘗試使用if let