【问题标题】:What does a UISegmentedControl return to its action on UIControlEventValueChanged?UISegmentedControl 对 UIControlEventValueChanged 的​​操作返回什么?
【发布时间】:2013-03-20 03:40:05
【问题描述】:

所以,我有一个 UISegmentedControl:

[control addTarget:self action:@selector(myAction) forControlEvents:UIControlEventValueChanged];

只是想知道如何找出已选择的细分(以便我可以执行适当的操作)。我知道它是这样的:

@selector(myAction:) 但是发送了什么?即:当我定义我的方法时,我必须定义什么?

谢谢。

【问题讨论】:

    标签: iphone uisegmentedcontrol


    【解决方案1】:

    获取所选项目...问题的第二部分

    -(IBAction) myAction:(id)sender{
        NSLog(@"myAction",nil);
    
        UISegmentedControl * control = sender;
        int selectedIndex = [control selectedSegmentIndex];
    }
    

    【讨论】:

    • 啊太棒了...我的问题是我正在尝试:int selectedIndex = [sender selectedSegmentIndex]; 而不是将其添加到对象中。 :) 太棒了,非常感谢。
    【解决方案2】:
    - (IBAction)myAction:(id)selector;
    

    选择器是一个 UISegmentedControl 对象。因此,如果您将一个操作绑定到两个 UISegmentedControl,则可能会导致两个 UISegmentedControl 不同。

    【讨论】:

    • 太棒了,所以 - 很抱歉很厚,但是如何决定选择哪个特定片段(尤其是当我有瞬间:YES)?谢谢
    【解决方案3】:

    还有更简单的方法:

    -(IBAction) myAction:(UISegmentedControl*)control {
       NSLog(@"selected index %d", control.selectedSegmentIndex);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      相关资源
      最近更新 更多