Justin Domnitz
Justin Domnitz

Reputation: 3307

Hidden status of WKInterfaceButton in WatchKit

Is there a way to check the current hidden status of WKInterfaceButton in WatchKit?

Upvotes: 3

Views: 886

Answers (3)

Byron
Byron

Reputation: 51

As an earlier answer states, you have to maintain your own variable to track whether or not a group, label, button etc. is hidden or not. A simple and elegant way to do this is by declaring your tracking variables with property observers like so:

@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)}}

We use a strong naming convention so that it is always obvious which object the tracking variable is assigned to. Once you have declared your variables and observers then in code you only ever refer to the tracking variables. You can query their current state and assign a new state as, for all intents and purposes, they can be regarded as a property of the object. E.g.

        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
        }

Our convention of adding "IsHidden" to the end of the tracking variable also makes it easier for us to move code between iOS and WatchOS as the changes needed are easy to locate and make.

Upvotes: 0

Victor Sigler
Victor Sigler

Reputation: 23451

The WKInterfaceButton class have a method inherited from WKInterfaceObject class entitled setHidden: that you can use to show/hide the button and with an auxiliar variable you can set programmatically the status of the WKInterfaceButton.

Something like the following example:

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)
   }
}

And whenever you want to know if the button is hidden/show you only need to check the variable buttonIsHidden.

I hope this help you.

Upvotes: 1

Arsen
Arsen

Reputation: 10951

It looks like you cannot do that. But you can store your own variable with state and use it.

Below you can check definition of button's parent class.

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
}

Upvotes: 1

Related Questions