Compare WKInterfaceLabel Text to Another NSString

六眼飞鱼酱① 提交于 2019-12-30 11:59:44

问题


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]) {

    }

回答1:


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.



来源:https://stackoverflow.com/questions/29703534/compare-wkinterfacelabel-text-to-another-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!