Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检测触发UIControl';s动作_Ios_Objective C_Cocoa Touch_Uicontrol - Fatal编程技术网

Ios 检测触发UIControl';s动作

Ios 检测触发UIControl';s动作,ios,objective-c,cocoa-touch,uicontrol,Ios,Objective C,Cocoa Touch,Uicontrol,我有一个带有动作方法回调的自定义UIControl子类。我希望在调整控件时在ui标签上显示控件元素的值,然后在用户停止调整控件时隐藏标签 因此,我连接了UIControlEventValueChanged和UIControlEventTouchUpInside的操作。两者都成功地调用了我的操作方法。但是,要根据操作在该方法中执行不同的操作,我需要知道哪个事件触发了该方法。我该怎么做?我查看了UIControl,没有看到明显的属性状态对于这两个操作似乎都返回1 比如说: - (void)h

我有一个带有动作方法回调的自定义
UIControl
子类。我希望在调整控件时在
ui标签上显示控件元素的值,然后在用户停止调整控件时隐藏标签

因此,我连接了
UIControlEventValueChanged
UIControlEventTouchUpInside
的操作。两者都成功地调用了我的操作方法。但是,要根据操作在该方法中执行不同的操作,我需要知道哪个事件触发了该方法。我该怎么做?我查看了
UIControl
,没有看到明显的属性<代码>状态
对于这两个操作似乎都返回
1

比如说:

    - (void)handleSlider1:(CustomSlider*)sender {

        if (sender.state == UIControlEventValueChanged) {
            // code
        } else {
            // different code
        }
    }

如果要使用相同的方法处理这两个事件,则可以检查控件的突出显示的属性:

if (sender.highlighted) {
   // slider is changing value (value changed)
} else {
   // slider has stopped changing value (touch up inside)
}

或者,您可以简单地创建两个单独的操作方法,并将每个事件连接到所需的方法。

通过将它们分别连接到单独的
iAction
s,您可以非常容易地区分这两个事件。然后,每个新操作将调用您的单个处理程序,传递适当的
UIControlEvent
值:

- (IBAction)sliderValueChanged:(CustomSlider *)slider
{
    [self handleSlider1:slider forEvent:UIControlEventValueChanged];   
}

- (IBAction)sliderValueChanged:(CustomSlider *)slider
{
    [self handleSlider1:slider forEvent:UIControlEventTouchUpInside];   
}

- (void)handleSlider1:(CustomSlider *)sender forEvent:(UIControlEvents)event
{
    if (event == UIControlEventValueChanged)
    //...
}

那不是我要的。问题是关于如何检测发送了什么类型的操作。至于对多个
UIControl
使用一个回调,我只是使用
。tag
=1,2,3,4…我不确定您为什么认为我需要
iAction
。我没有使用任何IB功能。只是编码。我最后做的是在我的
UIControl
子类的
endTrackingWithTouch:
方法中发送操作
UIControlEventTouchCancel
。除此之外(并增加复杂性),我将控件的主值设置为
-1
,作为我的viewController的哨兵,告知此操作是
UIControlEventTouchCancel
。之所以出现
-1
,正是因为当控件事件触发操作方法时,它似乎不会发送信息来识别它是哪个控件事件。无论它们是否为iActions,都可以使用此技术。