检测UISwitch中的更改 [英] Detecting a change in UISwitch

查看:103
本文介绍了检测UISwitch中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来不重要,但我注意到一些奇怪。我为一个UISwitch的Value Changed事件连接了一个处理程序。我将期望是每次处理程序被调用的交换机的值会改变。但实际上并不总是 。如果快速按下开关打开/关闭处理程序可以连续调用交换机的SAME状态(在我的具体应用程序这是一个问题)。所以我想知道是否有其他人注意到这种行为,并找出了一个很好的解决方案。

This sounds trivial but I'm noticing some weirdness. I've wired up a handler for the Value Changed event of a UISwitch. What I would expect is that each time the handler is called the value of the switch would change. But that's actually not always the case. If you press the switch rapidly on/off the handler can get called consecutively with the SAME state for the switch (in my specific application this is an issue). So I'm wondering if anyone else has noticed this behavior and has figured out a good solution.

推荐答案

   -(void) createSwitch
    {
        self.searchExistSearchNewSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,0,0)];
        [self.searchExistSearchNewSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:self.searchExistSearchNewSwitch];
    }
    - (void)switchValueChanged:(UISwitch *)theSwitch
    {
        BOOL flag = theSwitch.isOn;
    }

这篇关于检测UISwitch中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆