2016-11-23 89 views
1

我試圖做所有事情都沒有故事板,但添加我的標籤時仍然沒有零,但我不明白爲什麼。我確信它的死亡簡單,即時通訊是盲目的,但我只是不能看到它後很多谷歌搜索。Stackview標籤沒有以編程方式添加

視圖控制器:

class SquaresViewController: UIViewController { 

     let stackView = OAStackView() 
     let titleView = TitleView(frame: CGRect.zero) 

     @IBOutlet var squaresCollectionView: UICollectionView! 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      stackView.axis = .vertical 
      stackView.translatesAutoresizingMaskIntoConstraints = false 

      stackView.addArrangedSubview(titleView) 
      titleView.snp.makeConstraints { make in 
       make.height.equalTo(50) 
       make.top.equalTo(view.snp.top) 
       make.left.equalTo(view.snp.left) 
       make.right.equalTo(view.snp.right) 
      } 

     } 
    } 

titleview的:

import UIKit 

    class TitleView: UIView { 

     @IBOutlet weak var titleText: UILabel! 

     override init(frame: CGRect) { 
      super.init(frame: frame) 
      self.translatesAutoresizingMaskIntoConstraints = false 
      createView() 
     } 

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

     func createView() { 
      titleText = UILabel(frame: CGRect.zero) 
      titleText.translatesAutoresizingMaskIntoConstraints = false 

      titleText.text = "Tappy tap tap a square!" 
      titleText.textAlignment = .center 
      titleText.numberOfLines = 0 

      self.addSubview(titleText) 

      titleText.snp.makeConstraints { make in 
       make.edges.equalTo(self.snp.edges) 
      } 


     } 
    } 

在構建失敗的:

titleText.translatesAutoresizingMaskIntoConstraints = false 

有:

fatal error: unexpectedly found nil while unwrapping an Optional value 

任何幫助將是偉大的,即時通訊確定其簡單,但我不能再看到了。謝謝

回答

1

首先,我不明白爲什麼你爲titleText創建IBOutlet如果你不想使用故事板。如果你不想用故事板,改變實施:

var titleText : UILabel! 

其次,應用程序崩潰,因爲你要求它在還未被添加到層次結構中的視圖執行操作。爲了便於閱讀,我已經註釋了除了那些重要的行外的所有行。請看下圖:

func createView() { 
    //titleText = UILabel(frame: CGRect.zero) 
    titleText.translatesAutoresizingMaskIntoConstraints = false // <-- You are trying to perform an operation on titleText here 

    //titleText.text = "Tappy tap tap a square!" 
    //titleText.textAlignment = .center 
    //titleText.numberOfLines = 0 

    self.addSubview(titleText) // <-- but you are adding it to the view hierarchy here 

    //titleText.snp.makeConstraints { make in 
     //make.edges.equalTo(self.snp.edges) 
    //} 
} 

titleText已被添加到視圖層次,你可以調用它translatesAutoresizingMaskIntoConstraints之前,像這樣:

self.view.addSubview(titleText) 
titleText.translatesAutoresizingMaskIntoConstraints = false // <-- Here we are waiting until the view has been added to the hierarchy to perform operations on it 

所以嘗試改變實現看起來像這樣:

import UIKit 

class TitleView: UIView { 

    var titleText: UILabel! 

    override init(frame: CGRect) { 
     // leave as is, removed for space 
    } 

    required init?(coder aDecoder: NSCoder) { 
     // leave as is, removed for space 
    } 

    func createView() { 
     titleText = UILabel(frame: CGRect.zero) 
     titleText.text = "Tappy tap tap a square!" 
     titleText.textAlignment = .center 
     titleText.numberOfLines = 0 

     self.addSubview(titleText) 
     titleText.translatesAutoresizingMaskIntoConstraints = false 

     titleText.snp.makeConstraints { make in 
      make.edges.equalTo(self.snp.edges) 
     } 
    } 
} 
+0

非常感謝。我嘗試重新排序,但iboutlet是主要問題。 – Wazza

+0

沒問題! @WayneRumble – Wes

相關問題