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] objectAtIn开发者_运维百科dex: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?
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".
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.
Now there's a better solution for this. Anyone needing a custom switch should check https://github.com/domesticcatsoftware/DCRoundSwitch
Another option for a custom UISwitch: https://github.com/alexnauda/ERScrollSwitch
精彩评论