Heena
Heena

Reputation: 754

How to change text on UISwitch in iPhone?

I need to change the text on UISwitch and for that I have tried below code,

((UILabel *)[[[[[[_agreeAgb subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[_agreeAgb subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"Bar";

But It crashed at objectAtIndex

Is there any other way to do that other than making customized UISwitch?

Upvotes: 1

Views: 3425

Answers (4)

Alex Nauda
Alex Nauda

Reputation: 4536

Another option for a custom UISwitch: https://github.com/alexnauda/ERScrollSwitch

Upvotes: 0

bogdanh
bogdanh

Reputation: 66

Now there's a better solution for this. Anyone needing a custom switch should check https://github.com/domesticcatsoftware/DCRoundSwitch

Upvotes: 0

EmptyStack
EmptyStack

Reputation: 51374

Never use such hacking techniques to modify the built-in UIControls. In future, they may cease to work in case the implementation of the controls change in the successive updates of iOS.

If you want to customize UISwitch with your own text, you can instead you use UISegmentedControl with your new texts. For example, if you want to get the gender of the user, instead of changing the UISwitch's on and off labels text, you can use segmented control with two items "Male" & "Female".

Upvotes: 6

Stephen Darlington
Stephen Darlington

Reputation: 52565

You don't say which objectAtIndex: is causing the crash, but, in any case, it's moot: UISwitch does not currently allow customising of the labels.

Upvotes: 0

Related Questions