最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

[未解决]在UISegmentedControl的segmentValueChanged中获得当前选中的item项

iOS crifan 341浏览 0评论

折腾:

[已解决]swift实现左右切换的分段可点击的按钮选项

期间,需要去获得:

当前选中的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
&lt;UISegmentedControl: 0x7aecb3e0; frame = (0 180; 320 41); opaque = NO; tintColor = UIDeviceWhiteColorSpace 0 0; layer = &lt;CALayer: 0x7aecb5c0&gt;&gt;

(lldb) po sender
&lt;UISegmentedControl: 0x7aecb3e0; frame = (0 180; 320 41); opaque = NO; tintColor = UIDeviceWhiteColorSpace 0 0; layer = &lt;CALayer: 0x7aecb5c0&gt;&gt;

(lldb) po self.switchActionSegment.subviews
▿ 2 elements
  - [0] : &lt;UISegment: 0x7aecc720; frame = (160 0; 160 41); opaque = NO; layer = &lt;CALayer: 0x7ae9afd0&gt;&gt;
  - [1] : &lt;UISegment: 0x7aecbcb0; frame = (0 0; 159 41); opaque = NO; animations = { contents=&lt;CABasicAnimation: 0x7ae8c310&gt;; }; layer = &lt;CALayer: 0x7b89a660&gt;&gt;
</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..&lt;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 &gt; sender=&lt;UISegmentedControl: 0x7ae7c9d0; frame = (0 180; 320 41); opaque = NO; tintColor = UIDeviceWhiteColorSpace 0 0; layer = &lt;CALayer: 0x7ae7cc40&gt;&gt;, sender.selectedSegmentIndex=0
2016-05-31 15:48:59.968 [Verbose] [main] [LoginViewController.swift:237] segmentValueChanged &gt; [0] curSegment=&lt;UISegment: 0x7ae9f120; frame = (160 0; 160 41); opaque = NO; layer = &lt;CALayer: 0x7ae9f240&gt;&gt;
2016-05-31 15:48:59.969 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged &gt; [0] eachSubView=&lt;UIImageView: 0x7ae9eb40; frame = (160 0; 1 41); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; tag = -1030; layer = &lt;CALayer: 0x7ae9ec20&gt;&gt;
2016-05-31 15:48:59.970 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged &gt; [1] eachSubView=&lt;UISegmentLabel: 0x7aea3a90; frame = (62 10; 36 21.5); text = '注册'; opaque = NO; userInteractionEnabled = NO; layer = &lt;_UILabelLayer: 0x7aea39c0&gt;&gt;
2016-05-31 15:48:59.970 [Verbose] [main] [LoginViewController.swift:237] segmentValueChanged &gt; [1] curSegment=&lt;UISegment: 0x7c267fa0; frame = (0 0; 159 41); opaque = NO; animations = { contents=&lt;CABasicAnimation: 0x7aead490&gt;; }; layer = &lt;CALayer: 0x7c2ebaf0&gt;&gt;
2016-05-31 15:48:59.971 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged &gt; [0] eachSubView=&lt;UIImageView: 0x7ae9dfc0; frame = (159 0; 1 41); opaque = NO; autoresize = LM; userInteractionEnabled = NO; tag = -1030; layer = &lt;CALayer: 0x7ae9f030&gt;&gt;
2016-05-31 15:48:59.971 [Verbose] [main] [LoginViewController.swift:240] segmentValueChanged &gt; [1] eachSubView=&lt;UISegmentLabel: 0x7aea2e20; frame = (61.5 10; 36 21.5); text = '登录'; opaque = NO; userInteractionEnabled = NO; layer = &lt;_UILabelLayer: 0x7aea3050&gt;&gt;
2016-05-31 15:48:59.972 [Verbose] [main] [LoginViewController.swift:257] segmentValueChanged &gt; selectedSegment=&lt;UISegment: 0x7ae9f120; frame = (160 0; 160 41); opaque = NO; layer = &lt;CALayer: 0x7ae9f240&gt;&gt;
</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项

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
14 queries in 0.258 seconds, using 10.32MB memory