当前位置: 代码迷 >> Iphone >> 怎么修改UISwitch的text
  详细解决方案

怎么修改UISwitch的text

热度:60   发布时间:2016-04-25 06:42:20.0
如何修改UISwitch的text?
网上一搜都是这样说的:

-------------------------------

为要更改Text的UISwitch建立IB连接:例如mySwitch,在代码中改变文字:
 
((UILabel *)[[[[[[self.mySwitch subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"Foo";
 
((UILabel *)[[[[[[self.mySwitch subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"Bar"; 

-------------------------------

全是一样,按照这个试了一下,报错 数组越界

这个怎么做

------解决方案--------------------
uiswich
内部的subviews 好像没有

你实在要用就自己自定义算了。功能一样能实现,效果就差了一点就是。。。

表示无力。。。



------解决方案--------------------
貌似是只能自己定义、、

------解决方案--------------------
if (self.numberOfSegments) {
for (id seg in [self subviews]) {
for (id label in [seg subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *temp = label;
[temp setText:@"text"];
}
}
}
}

------解决方案--------------------
遍历UISwitch的视图树,输出为
UISwitch:UIControl
-UIView:UIResponder
-UIView:UIResponder
没有了
UISwitch:UIControl
-_UISwitchSlider:UISlider
--UIImageView:UIView
--UIImageView:UIView
--UIView:UIResponder
---UILabel:UIView
---UILabel:UIView
--UIImageView:UIView
所以这个方法是行不通了,换衍生或自定义?
------解决方案--------------------
自定义!!!!!
  相关解决方案