Greg Lukosek
Greg Lukosek

Reputation: 1792

WatchKit how to check current state of WKInterfaceSwitch

Im trying to figure out if my instance of WKInterfaceSwitch is currently in on or off position

Upvotes: 4

Views: 1230

Answers (1)

BalestraPatrick
BalestraPatrick

Reputation: 10144

You can't do that. You need to track with a variable the status of the WKInterfaceSwitch in your code.

Let's say your default value for a WKInterfaceSwitch is false.

In your awakeWithContext: method do this:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    self.switchStatus = NO;
}

In Objective-C you would declare a property with a BOOL value.

@property (nonatomic, assign) BOOL switchStatus;

Then create an action from your Switch object to your header file.

- (IBAction)valueChanged:(BOOL)value;

And in the implementation file write.

- (IBAction)valueChanged:(BOOL)value {
     self.switchStatus = value;
}

You are now able to check the status of your Switch by just using self.switchStatus for example like this:

    NSLog(@"Switch is now: %@", self.switchStatus ? @"true" : @"false");

I hope this helps.

Upvotes: 4

Related Questions