WatchKit中WKInterfaceButton的隐藏状态

时间:2015-10-02 20:20:29

标签: ios swift watch-os-2

有没有办法检查WatchKit中WKInterfaceButton的当前隐藏状态?

3 个答案:

答案 0 :(得分:1)

看起来你不能这样做。但是你可以用状态存储你自己的变量并使用它。

您可以在下面查看按钮父类的定义。

public class WKInterfaceObject : NSObject {

    public func setHidden(hidden: Bool)
    public func setAlpha(alpha: CGFloat)

    @available(watchOS 2.0, *)
    public func setHorizontalAlignment(horizontalAlignment: WKInterfaceObjectHorizontalAlignment)
    @available(watchOS 2.0, *)
    public func setVerticalAlignment(verticalAlignment: WKInterfaceObjectVerticalAlignment)

    public func setWidth(width: CGFloat)
    public func setHeight(height: CGFloat)
    @available(watchOS 2.0, *)
    public func setRelativeWidth(width: CGFloat, withAdjustment adjustment: CGFloat)
    @available(watchOS 2.0, *)
    public func setRelativeHeight(height: CGFloat, withAdjustment adjustment: CGFloat)

    @available(watchOS 2.0, *)
    public func sizeToFitWidth()
    @available(watchOS 2.0, *)
    public func sizeToFitHeight()

    public var interfaceProperty: String { get } // same as controller's property name
}

答案 1 :(得分:1)

WKInterfaceButton类有一个继承自WKInterfaceObject类的方法,名为setHidden:,您可以使用它来显示/隐藏按钮,并使用辅助变量,您可以通过编程方式设置状态WKInterfaceButton

类似以下示例:

class InterfaceController: WKInterfaceController {

   @IBOutlet var button: WKInterfaceButton!     

   var buttonIsHidden: Bool!

   override func awakeWithContext(context: AnyObject?) {
      self.changeStatusOfButton(true)
   }

   private func changeStatusOfButton(status: Bool) {

      // set programmatically the status of the button to hide/show 
      self.activityButton.setHidden(status)

      // save the current status
      self.buttonIsHidden = status
   }

   private func showButtonAgain() {
       self.changeStatusOfButton(false)
   }
}

每当您想知道按钮是否隐藏/显示时,您只需要检查变量buttonIsHidden

我希望这对你有所帮助。

答案 2 :(得分:0)

如先前的回答所述,您必须维护自己的变量以跟踪是否隐藏了组,标签,按钮等。一种简单而优雅的方法是通过使用属性观察器声明跟踪变量,如下所示:

@IBOutlet weak var btnChange: WKInterfaceButton!
@IBOutlet weak var lblPending: WKInterfaceLabel!
@IBOutlet weak var lblConfirmed: WKInterfaceLabel!

// Property observers.  Set initial values to match storyboard / initial setup
var btnChangeIsHidden = false {didSet{btnChange.setHidden(btnChangeIsHidden)}}
var lblPendingIsHidden = true {didSet{lblPending.setHidden(lblPendingIsHidden)}}
var lblConfirmedIsHidden = true {didSet{lblConfirmed.setHidden(lblConfirmedIsHidden)}}

我们使用严格的命名约定,以便始终很清楚地将跟踪变量分配给哪个对象。一旦声明了变量和观察者,然后在代码中就只能引用跟踪变量。您可以查询它们的当前状态并分配新的状态,因为出于所有意图和目的,它们可以被视为对象的属性。例如

        if !self.lblPendingIsHidden {
        // In a pending state; reset to status before attempted change
            // Toggle button / label
            self.btnChangeIsHidden = !self.btnChangeIsHidden
            self.lblPendingIsHidden = !self.lblPendingIsHidden
        } else {
            // Change was applied
            self.btnChangeIsHidden = false
            self.lblConfirmedIsHidden = true
        }

我们在跟踪变量的末尾添加“ IsHidden”的约定也使我们可以更轻松地在iOS和WatchOS之间移动代码,因为所需的更改很容易找到和进行。