简体   繁体   中英

WKInterfaceLabel in custom WKInterfaceTable row unwrapping to nil

The title sums it up. I've tried disconnecting/reconnecting all the IBOutlets. Here's the code on my InterfaceController where I set up the WKInterfaceTable:

func setupLoadingTable () {
    self.myTable.setNumberOfRows(1, withRowType:"universalRowID")

    for (var i = 0; i<self.myTable.numberOfRows; i++){
        var row = self.myTable.rowControllerAtIndex(i) as! UniversalRowView
        row.mainTitle.setText("Hello world") //THIS THROWS AN ERROR
    }
}

Here's the code for my UniversalRowView which is my custom row class:

import WatchKit

class UniversalRowView: NSObject {

    @IBOutlet weak var mainTitle: WKInterfaceLabel!
    @IBOutlet weak var subTitle: WKInterfaceLabel!

}

The IBOutlets for the Table is properly connected. The row on Storyboard is set to type UniversalRowView and outlets for both labels are properly connected.

The line:

row.mainTitle.setText("Hello world")

throws the following error: "fatal error: unexpectedly found nil while unwrapping an Optional value"

Also, when I comment that code out, I don't see the stock WKInterfaceLabels on the simulator as they appear on the Storyboard. I feel like this is an error as well but I don't know what to make of it.

Edit : Calling setupLoadingTable() in willActivate()

I figured it out! It seems like there is an attribute in the view of both WKInterfaceTable and WKInterfaceLabel called "38mm Installed". The problem was, I was running the simulator on 42mm but I only had 38mm checked .

Once I checked both attributes, everything worked as planned!

Here's a screenshot of what I'm referring to:

在此处输入图片说明

我遇到了同样的问题,对我来说,是因为我为WKInterfaceLabel title命名了IBOutlet,这也证明了NSObject中方法的名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM