objective-cwatchkitapple-watchwkinterfacelabel

Compare WKInterfaceLabel Text to Another NSString


I know there is no getter method for WKInterfaceLabel, but is there another way for me to compare the label text to another string? If this wasn't a watch app and I was using UILabel I could just do this:

if ([self.label.text isEqualToString:someString]) {

    }

Solution

  • There's no supported way to get the text, just as you said, however you may want to use the accessibility elements as "option".

    Here's the idea:

    When self.label text is set (either in code or storyboard) also set the corresponding accessibility label/value. When you need to read/update the label text, just make sure you use the accessibility values instead.

    self.label.text = @"foo";
    self.label.accessibilityValue = @"foo";
    
    if ([self.label.accessibilityValue isEqualToString:someString]) {
       self.label.text = @"bar";
       self.label.accessibilityValue = @"bar";
       ...
    }
    

    Plus it's how you would use accessibility anyway so it's legal. There may be other ways to accomplish, but this seems to be the quickest and safest way to do what you want.