xcode 更改 iOS 4.2 中的 UISwitch 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4806743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing of UISwitch text in iOS 4.2
提问by user510951
I am trying to change the text in UISwitch. The sample from the website ( Changing the text on a UISwitch) works fine but when I upgrade my xcode to 3.2.5 and iOS 4.2, the application crash when the functions is being called to change the text. I am using the following example from the website.
我正在尝试更改 UISwitch 中的文本。来自网站的示例(更改 UISwitch 上的文本)工作正常,但是当我将 xcode 升级到 3.2.5 和 iOS 4.2 时,当调用函数来更改文本时应用程序崩溃。我正在使用网站上的以下示例。
eg.
例如。
((UILabel *)[[[[[[_agreeAgb subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[_agreeAgb subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"Bar";
The exception thrown is because null object is encountered.
抛出异常是因为遇到空对象。
Thanks.
谢谢。
回答by Lily Ballard
Don't do this. Manipulating the private view hierarchy of framework controls is absolutely unsupported, and can cause incompatibility with OS updates. Especially as your code does absolutely no verification of the hierarchy, so you can very easily crash if the number or type of subviews doesn't match what you were expecting.
不要这样做。绝对不支持操纵框架控件的私有视图层次结构,并且可能导致与操作系统更新不兼容。特别是因为您的代码绝对不验证层次结构,所以如果子视图的数量或类型与您期望的不匹配,您很容易崩溃。
回答by Matt Blackmon
I found success in 4.2 with this code from here. It subclasses UISlider (not UISwitch) to achieve an effect that looks the same as a customized UISwitch.
我使用这里的代码在 4.2 中取得了成功。它继承了 UISlider(而不是 UISwitch)以实现与自定义 UISwitch 看起来相同的效果。
回答by Dan F
There still doesn't seem to be a supported way to change the TEXT yet, but you can change what is displayed by setting the onImage
and offImage
properties, introduced in iOS 6
似乎仍然没有一种受支持的方式来更改文本,但是您可以通过设置onImage
和offImage
属性来更改显示的内容,在 iOS 6 中引入
回答by Slipp D. Thompson
Try using a customizable open source UISwitch replacement.
尝试使用可定制的开源 UISwitch 替代品。
This one seems pretty good: DCRoundSwitch on GitHub, but there are many more on GitHub if that one doesn't work.
这个看起来不错:GitHub 上的 DCRoundSwitch,但如果那个不起作用,GitHub 上还有更多。
This type of approach doesn't have the same incompatibility issues because it implements the switch's functionality and drawing itself and only relies on the core functionality UIControl base class.? The only thing you might need to update from time to time is the library you're using (DCRoundSwitch), however, base functionality like UIControl and Quartz drawing are quite established and rarely change so this can safely be considered a rare and minimal risk factor.
这种方法没有同样的不兼容问题,因为它实现了开关的功能和绘图本身,并且只依赖于 UIControl 基类的核心功能。?您可能需要不时更新的唯一内容是您正在使用的库 (DCRoundSwitch),但是,UIControl 和 Quartz 绘图等基本功能已经相当成熟且很少更改,因此可以安全地将其视为罕见且最小的风险因素.