簡體   English   中英

在viewDidLoad中從Nib解包nil的Swift錯誤

[英]Swift error unwrapping nil from Nib in viewDidLoad

得到一些奇怪的結果,我正在加載我作為自定義警報視圖創建的 nib 文件。 但是,當它嘗試更改自定義警報視圖中 UILabel 的文本時,它在 viewDidLoad 方法中失敗。 (獲取解包零錯誤)

當我轉到 nib 文件時,所有插座都已連接(顯示旁邊突出顯示的點)。 文件的所有者也被設置為正確的類名。

然而,當代碼失敗並啟動調試器時,所有的出口都是空的。 這段代碼出現在 viewDidAppear 中,所以此時不​​應該加載出口和所有 UI 嗎?

附帶信息:另一個奇怪的部分是我正在兩個不同的設備上測試這個。 一個運行 iOS 8.4,另一個運行 iOS 9.1(測試版)。 iOS 9.1 設​​備運行正常,不會拋出此 nil 錯誤。 iOS 8.4 設備是引發錯誤的設備。 運行 Xcode 6.4 swift 1.2。 為了讓代碼在我的 iOS 9.1 上運行,我在我的設備連接到計算機的情況下打開 Xcode 7,然后關閉並重新打開 Xcode 6.4,它允許我將目標 iOS 設置為 9.1。

更新

代碼失敗的圖像在此處輸入圖片說明

Nib 文件視圖控制器:

@IBOutlet var messageLabel: UILabel!
@IBOutlet var notificationHeightConstraint: NSLayoutConstraint!
@IBOutlet var notificationWidthConstraint: NSLayoutConstraint!
@IBOutlet var singleButton: UIButton!
@IBOutlet var rightButton: UIButton!
@IBOutlet var leftButton: UIButton!
@IBOutlet var middleButtonSpacerView: UIView!

var buttonDelegate: NotificationButtonDelegate?
var grayView: UIView!
var buttonTitles = [String]()
var notificationWidth: CGFloat!
var notificationHeight: CGFloat = 200.0
var hideProgressView = false
var hideNotificationView = false
var currentProgress: CGFloat = 0.0
var messageLabelText = ""


required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    messageLabel.text = messageLabelText

    if buttonTitles.count > 0 {
        if buttonTitles.count == 1 {
            rightButton.hidden = true
            leftButton.hidden = true
            middleButtonSpacerView.hidden = true
            singleButton.setTitle("\(buttonTitles[0])", forState: UIControlState.Normal)
        } else {
            leftButton.setTitle("\(buttonTitles[0])", forState: UIControlState.Normal)
            rightButton.setTitle("\(buttonTitles[1])", forState: UIControlState.Normal)
        }
    }


    if self.hideProgressView {
        self.progressView.hidden = true
        if messageLabel.bounds.height >= 90 {
            notificationHeightConstraint.constant = 225
        } else {
            notificationHeightConstraint.constant = 200.0
        }
    }

    if self.hideNotificationView {
        self.notificationView.hidden = true
        self.activityIndicator.startAnimating()
        notificationHeightConstraint.constant = 160
    }

    self.notificationWidthConstraint.constant = notificationWidth
    self.view.needsUpdateConstraints()

    // Do any additional setup after loading the view.

}

主視圖控制器中的自定義警報視圖類初始化

var notificationPopup: NotificationPopupViewController!
var progressPopup: NotificationPopupViewController!

override func viewDidLoad() {
    super.viewDidLoad()
    self.hasError = false
    notificationPopup = NotificationPopupViewController()
    progressPopup = NotificationPopupViewController()

在主視圖控制器中調用以呈現自定義警報視圖

@IBAction func configureSystemButtonTapped(sender: AnyObject) {

    var prefs: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var hostedSSID = prefs.valueForKey("HostedSSID") as? String
    var currentSSID = sureVue.getUserSSID()

    if currentSSID != hostedSSID || currentSSID == "" {
        self.notificationPopup.showInView(self.view, withMessage: "You are not connected to the device's wifi.\nFollow instructions on launch page to connect.", animated: false, buttonLabels: ["Ok"])

設置自定義警報視圖的 nib viewController 中的方法

    func showInView(aView: UIView!, withMessage message: String!, animated: Bool, buttonLabels:[String]) {
    self.hideNotificationView = false
    self.hideProgressView = true
    self.buttonTitles = buttonLabels
    notificationWidth = aView.bounds.width * 0.925
    grayView = UIView(frame: aView.frame)
    grayView.backgroundColor = UIColor(red: 170/255.0, green: 170/255.0, blue: 170/255.0, alpha: 0.75)
    grayView.center = CGPoint(x: aView.bounds.width/2, y: aView.bounds.height/2)
    grayView.addSubview(self.view)
    self.view.center = CGPoint(x: aView.bounds.width/2, y: aView.bounds.height/2)
    aView.addSubview(grayView)
    messageLabelText = message


    if animated
    {
        self.showAnimate()
    }
}

根據評論,我認為問題在於 self.notificationPopup 的初始化方式。 您需要使用init(nibName: ...) ,而不是notificationPopup = NotificationPopupViewController()以便從筆尖加載實例並設置其出口。

現有代碼在 iOS 9 中運行正常,因為 Apple 更改了默認 nibNames,如果您在初始化 UIViewController 時未指定該名稱,則使用該名稱。 請參閱iOS9 發行說明中UIKit 下的說明

如果init(nibName: ...)被正確使用,請嘗試 -

  1. 清理/重建項目
  2. 從模擬器/設備中刪除應用程序

有時,當添加新文件時,Xcode 7 會錯誤地構建應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM