4

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.

Community
  • 1
  • 1
user510951
  • 1,295
  • 3
  • 10
  • 10

5 Answers5

14

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.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
7

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.

Matt Blackmon
  • 1,246
  • 1
  • 11
  • 19
  • +1 here and -1 on the answer above. True that you cannot do it the way you were, but the real answer to your question is how to achieve a UISwitch-equivalent with a different label. Another option is here http://osiris.laya.com/projects/rcswitch/ – Rhubarb Oct 09 '12 at 10:36
  • Just a note to people that use this solution: it requires you to provide your own switch imagery. When iOS switched to the new style switches (iOS 5?), any apps using this would have switches that looked *old*. So, you should be sure the visually check the consistency of this solution, as new iOS versions appear. – Nate Mar 07 '13 at 01:30
1

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

Dan F
  • 17,654
  • 5
  • 72
  • 110
0

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.

Slipp D. Thompson
  • 33,165
  • 3
  • 43
  • 43
0

The latest version of XCode Beta 4 just added support for changing text on UISwitch. And also a SwtichStyle that I haven't investigated just what styles are out there.

Bookworm
  • 1
  • 2