WatchKit willActivate() function. Xcode doesn'

2019-09-05 15:04发布

问题:

I have zero errors. Only problem is nothing displays in my simulator. I'm guessing it's because I don't have anything in awakeWithContext? I did have self.view.insertSubview(heartSymbol, atIndex: 1) but had an Xcode error. http://i.imgur.com/wzOIR2v.png "InterfaceController doesn't have a member named 'view'". I also tried self.heartSymbol = currentBeatPattern.heartImage in the awakeWithContext() function but still no luck. Also I do not have a willActivate() function right now so is that ok for now?

In the beginning stages of coding this app I simply want to cycle through the 5 heartImages in the if loop in the newBeat() function then display them in the heartSymbol IBOutlet.

import WatchKit
import Foundation


struct BeatPattern {
var heartImage = WKInterfaceImage()
var description = "Normal"
var bpm = 80

var duration: Double {
    return 60.0 / Double(bpm)
}
}


class InterfaceController: WKInterfaceController {

@IBOutlet weak var heartSymbol: WKInterfaceImage!

var currentBeatPattern = BeatPattern()
var currentBeatPatternIndex = 0

var beatPatternsArray = [
    BeatPattern(heartImage: redHeartFast, description: "Fast", bpm: 180),
    BeatPattern(heartImage: yellowHeartElevated, description: "Elevated", bpm: 140),
    BeatPattern(heartImage: greenHeartNormal, description: "Normal", bpm: 80)]




override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    self.view.insertSubview(heartSymbol, atIndex: 1)   // Error

}

override func willActivate() {
    super.willActivate()

}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()

    newBeat()

    NSTimer.scheduledTimerWithTimeInterval(8,
        target: self,
        selector: Selector("newBeat"),
        userInfo: nil,
        repeats: true)

    //beat()

}

回答1:

A WKInterfaceController is not directly related to a UIInterfaceController. It has no view property. WatchKit has no UIViews at all - it has WKInterfaceElements, but there is no way to dynamically insert them at runtime. All of your interface elements must be set up in Xcode's Interface Builder pane, although you can hide and show views at runtime and change a few settings, like strings or images.