比较WKInterfaceLabel文本和另一个NSString [英] Compare WKInterfaceLabel Text to Another NSString

查看:100
本文介绍了比较WKInterfaceLabel文本和另一个NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道WKInterfaceLabel没有getter方法,但是我还有另一种将标签文本与另一个字符串进行比较的方法吗?如果这不是手表应用程序,而我正在使用UILabel,则可以执行以下操作:

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

    }

推荐答案

没有像您所说的那样获得文本的受支持方法,但是您可能希望将可访问性元素用作选项".

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

这里是个主意:

设置self.label文本(在代码或情节提要中)时,还要设置相应的辅助功能标签/值.当您需要阅读/更新标签文本时,只需确保使用辅助功能值即可.

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.

这篇关于比较WKInterfaceLabel文本和另一个NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆