WatchKit, how to check the current status of WKInterfaceSwitch

I am trying to find out if my WKInterfaceSwitch instance is turned on

+4
source share
1 answer

You cannot do this. You need to track with a variable the status WKInterfaceSwitchin your code.

Say your default value for WKInterfaceSwitchis false.

In your method awakeWithContext: do the following:

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

    self.switchStatus = NO;
}

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

@property (nonatomic, assign) BOOL switchStatus;

Then create an action from the Switch object in the header file.

- (IBAction)valueChanged:(BOOL)value;

And write in the implementation file.

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

Now you can check the status of your switch by simply using self.switchStatus, for example, as follows:

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

Hope this helps.

+4

All Articles