2017-06-26 22 views
0

進口UIKit的,並試圖切換周圍,其中以編程方式初始化按鈕的顏色後,我似乎無法找出爲什麼崩潰發生在類符合WKInterfaceControllerWatchKit的setBackground顏色碰撞

import UIKit 
import WatchKit 

class InterfaceController: WKInterfaceController { 

    @IBOutlet fileprivate var myButton : WKInterfaceButton! 

    override func willActivate() { // About to be visible to user 
    super.willActivate() 

    self.myButton.setBackgroundColor(UIColor.red) // CRASH 
    } 
} 

enter image description here

enter image description here

enter image description here

+0

你當前的代碼行沒有錯,顯示錯誤信息。 –

+0

@RashwanL只是附加圖片 – lifewithelliott

回答

1

原因在於未使用的斷點。

總之:

使用awake(withContext:)改變UI:

When creating an interface controller, WatchKit instantiates the class and calls its init() method followed shortly by its awake(withContext:) method. Use those methods to initialize variables, load data, and configure the items in your storyboard scene. If WatchKit passes a valid object to the awake(withContext:) method, use the information in that object to customize the initialization process.

不能使用willActivate()改變背景顏色等:

The willActivate() method lets you know when your interface is active. Use the willActivate() method to perform any last minute tasks, such as checking for updates to your content. (Do not use it for your primarily initialization.)

而且總是用weak網點:

@IBOutlet fileprivate weak var myButton : WKInterfaceButton! 

並檢查您的插座是否已連接。

+0

該按鈕沒有返回零,所以我不知道問題是什麼,關於'弱'。在iOS中,我知道當連接默認使用「weak」的插座時,在WatchOS中不使用,所以我不想添加蘋果認爲應該不存在的東西。 – lifewithelliott

+0

你可以添加錯誤信息文本嗎? –

+0

除了guard語句返回一個打印語句外,我不知道如何添加其他錯誤捕捉類型的文本。錯誤消息文本是什麼意思,你能否提供一個例子? – lifewithelliott