Changing of UISwitch text in iOS 4.2

穿精又带淫゛_ 提交于 2019-11-27 08:00:58

问题


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.

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.


回答1:


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.




回答2:


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.




回答3:


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

http://developer.apple.com/library/ios/documentation/uikit/reference/UISwitch_Class/Reference/Reference.html




回答4:


Try using a customizable open source UISwitch replacement.

This one seems pretty good: DCRoundSwitch on GitHub, but there are many more on GitHub if that one doesn't work.

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.



来源:https://stackoverflow.com/questions/4806743/changing-of-uiswitch-text-in-ios-4-2

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