折腾:
期间,需要去获得:
当前选中的segment
uisegmentedcontrol get selected segment
iphone – get string value from UISegmentedControl – Stack Overflow
ios – How to get value from segmented control to know which one was picked – Stack Overflow
iphone – Selected segment of UISegmentedControl – Stack Overflow
iphone – UISegmentedControl selected index always 0? – Stack Overflow
uisegmentedcontrol selected UISegmentedControlSegment
uisegmentedcontrol current UISegmentedControlSegment
uisegmentedcontrol selected item
通过调试输出:
<code>(lldb) po self.switchActionSegment <UISegmentedControl: 0x7aecb3e0; frame = (0 180; 320 41); opaque = NO; tintColor = UIDeviceWhiteColorSpace 0 0; layer = <CALayer: 0x7aecb5c0>> (lldb) po sender <UISegmentedControl: 0x7aecb3e0; frame = (0 180; 320 41); opaque = NO; tintColor = UIDeviceWhiteColorSpace 0 0; layer = <CALayer: 0x7aecb5c0>> (lldb) po self.switchActionSegment.subviews ▿ 2 elements - [0] : <UISegment: 0x7aecc720; frame = (160 0; 160 41); opaque = NO; layer = <CALayer: 0x7ae9afd0>> - [1] : <UISegment: 0x7aecbcb0; frame = (0 0; 159 41); opaque = NO; animations = { contents=<CABasicAnimation: 0x7ae8c310>; }; layer = <CALayer: 0x7b89a660>> </code>
感觉到:
UISegmentedControl的子item,应该叫做:UISegment
uisegmentedcontrol selected UISegment
去代码中,没有找到有UISegment这个东西。。。
然后好像是:
当前选中0,结果获得的subview是1的segment。。。
uisegmentedcontrol ValueChanged selectedSegmentIndex
objective c – How to Detect Change in UISegmentedControl from a Separate IBAction – Stack Overflow
ios – Change UISegmentedControl selected index or value programatically – Stack Overflow
最后,也还是没有获得:当前选中的segment的view。。。
而是通过代码:
<code>let SwitchActionIndexLogin:Int = 0 let SwitchActionIndexRegister:Int = 1 let actionList:[String] = ["登录", "注册"] var switchActionSegment:UISegmentedControl //2. Login or Register switch self.switchActionSegment.backgroundColor = AppMainColor self.switchActionSegment.insertSegmentWithTitle(actionList[SwitchActionIndexLogin], atIndex: SwitchActionIndexLogin, animated: false) self.switchActionSegment.insertSegmentWithTitle(actionList[SwitchActionIndexRegister], atIndex: SwitchActionIndexRegister, animated: false) self.switchActionSegment.setTitleTextAttributes([NSFontAttributeName : SwitchActionTitleFont, NSForegroundColorAttributeName : UIColor.lightTextColor()], forState: UIControlState.Normal) self.switchActionSegment.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName : SwitchActionTitleFont], forState: UIControlState.Selected) self.switchActionSegment.addTarget(self, action: #selector(self.segmentValueChanged(_:)), forControlEvents: UIControlEvents.ValueChanged) func segmentValueChanged(sender:UISegmentedControl) { gLog.debug("sender=\(sender), sender.selectedSegmentIndex=\(sender.selectedSegmentIndex)") for index in 0..<self.switchActionSegment.numberOfSegments { let curSegment = self.switchActionSegment.subviews[index] gLog.verbose("[\(index)] curSegment=\(curSegment)") for (subviewIdx, eachSubView) in curSegment.subviews.enumerate() { gLog.verbose("[\(subviewIdx)] eachSubView=\(eachSubView)") if eachSubView.tag == LoginSwitchActionSelectedIndicatorTag { eachSubView.removeFromSuperview() } } } let selectedSegment = self.switchActionSegment.subviews[sender.selectedSegmentIndex] gLog.verbose("selectedSegment=\(selectedSegment)") </code>
通过log:
<code>2016-05-31 15:48:59.968 [Debug] [main] [LoginViewController.swift:233] segmentValueChanged > sender=<UISegmentedControl: 0x7ae7c9d0; frame = (0 180; 320 41); opaque = NO; tintColor = UIDeviceWhiteColorSpace 0 0; layer = <CALayer: 0x7ae7cc40>>, sender.selectedSegmentIndex=0 2016-05-31 15:48:59.968 [Verbose] [main] [LoginViewController.swift:237] segmentValueChanged > [0] curSegment=<UISegment: 0x7ae9f120; frame = (160 0; 160 41); opaque = NO; layer = <CALayer: 0x7ae9f240>> 2016-05-31 15:48:59.969 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged > [0] eachSubView=<UIImageView: 0x7ae9eb40; frame = (160 0; 1 41); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; tag = -1030; layer = <CALayer: 0x7ae9ec20>> 2016-05-31 15:48:59.970 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged > [1] eachSubView=<UISegmentLabel: 0x7aea3a90; frame = (62 10; 36 21.5); text = '注册'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7aea39c0>> 2016-05-31 15:48:59.970 [Verbose] [main] [LoginViewController.swift:237] segmentValueChanged > [1] curSegment=<UISegment: 0x7c267fa0; frame = (0 0; 159 41); opaque = NO; animations = { contents=<CABasicAnimation: 0x7aead490>; }; layer = <CALayer: 0x7c2ebaf0>> 2016-05-31 15:48:59.971 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged > [0] eachSubView=<UIImageView: 0x7ae9dfc0; frame = (159 0; 1 41); opaque = NO; autoresize = LM; userInteractionEnabled = NO; tag = -1030; layer = <CALayer: 0x7ae9f030>> 2016-05-31 15:48:59.971 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged > [1] eachSubView=<UISegmentLabel: 0x7aea2e20; frame = (61.5 10; 36 21.5); text = '登录'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7aea3050>> 2016-05-31 15:48:59.972 [Verbose] [main] [LoginViewController.swift:257] segmentValueChanged > selectedSegment=<UISegment: 0x7ae9f120; frame = (160 0; 160 41); opaque = NO; layer = <CALayer: 0x7ae9f240>> </code>
可以看出来:
index=0 中的subview[1]是text = ‘注册’
index=1 中的subview[1]是text = ‘登录’
即,虽然当前获得的index是正常的,所要的0
但是通过index所获得的subview却是相反的:
不是当前所选中的segment,而是另外一个segment的view
最后还是没有好办法去解决,只能用最最笨的办法了:
把:
<code>let selectedSegment = self.switchActionSegment.subviews[sender.selectedSegmentIndex] </code>
换成:
<code> var realSubviewIdx = sender.selectedSegmentIndex if sender.selectedSegmentIndex == SwitchActionIndexLogin { realSubviewIdx = SwitchActionIndexRegister } else { realSubviewIdx = SwitchActionIndexLogin } // let selectedSegment = self.switchActionSegment.subviews[sender.selectedSegmentIndex] let selectedSegment = self.switchActionSegment.subviews[realSubviewIdx] </code>
才得到真正的segment的subview。。。
转载请注明:在路上 » [未解决]在UISegmentedControl的segmentValueChanged中获得当前选中的item项