UISwitch快速切换多次

发布于 2024-08-29 09:23:40 字数 1400 浏览 10 评论 0原文

我正在玩我的应用程序,并且我非常快地切换 UISwitch(谁能抗拒?)。因此,我非常快地切换了 10-15 次,一个数组应该包含我切换的表视图单元格中的数据,要么具有同一单元格的额外副本,要么只有一个副本(正确的情况)或里面根本没有数据。如果以正常速度切换,该应用程序可以正常工作。

我很确定这种情况不会真正发生在我的用户身上,但我仍然很好奇为什么会发生这种情况。

谢谢, Teja

编辑:添加代码。每次切换开关时都会调用此方法。

-(void)saveOptionChanged:(id) sender
{
UISwitch *sw = (UISwitch *) sender;
//int tag = current.tag;
BOOL status = (BOOL) [sw isOn];
NSInteger tag = sw.tag;
NSInteger row1 = tag%1000;
NSInteger section1 = (tag - row1)/1000;
DiagDetails *temp = [preferences getDiagElementAt:section1 row:row1];
temp.ICD9Code= [temp.ICD9Code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
temp.ICD9Desc= [temp.ICD9Desc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Adding %@ - %@",temp.ICD9Code, temp.ICD9Desc);
NSLog(@"tag = %ld, row = %ld, section = %ld",tag,row1,section1);
if(status) {
    BOOL returnVal = YES;
    returnVal = [currentPatient addICD9Code:temp];
    if(!returnVal) {
        UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Diagnosis add error" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [error show];
        [error release];
        [sw setOn:NO];
    }
}
else {
    DiagDetails *temp = [preferences getDiagElementAt:section1 row:row1];
    [currentPatient removeICD9Code:temp.ICD9Code];
}
}

I was playing around my app and I was toggling a UISwitch really fast (who can resist?). So, I toggled it really fast for 10-15 times, an array that should contain the data from the table view cell that I have my switch in, either has extra copies of the same cell or just one copy (the correct case) or no data in it at all. The app works fine if it's toggled at normal speed.

I'm pretty sure this scenario won't really happen with my users, but I'm still curious as to why it's happening.

Thanks,
Teja

Edit: Adding code. This method gets called each time the switch is toggled.

-(void)saveOptionChanged:(id) sender
{
UISwitch *sw = (UISwitch *) sender;
//int tag = current.tag;
BOOL status = (BOOL) [sw isOn];
NSInteger tag = sw.tag;
NSInteger row1 = tag%1000;
NSInteger section1 = (tag - row1)/1000;
DiagDetails *temp = [preferences getDiagElementAt:section1 row:row1];
temp.ICD9Code= [temp.ICD9Code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
temp.ICD9Desc= [temp.ICD9Desc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Adding %@ - %@",temp.ICD9Code, temp.ICD9Desc);
NSLog(@"tag = %ld, row = %ld, section = %ld",tag,row1,section1);
if(status) {
    BOOL returnVal = YES;
    returnVal = [currentPatient addICD9Code:temp];
    if(!returnVal) {
        UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Diagnosis add error" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [error show];
        [error release];
        [sw setOn:NO];
    }
}
else {
    DiagDetails *temp = [preferences getDiagElementAt:section1 row:row1];
    [currentPatient removeICD9Code:temp.ICD9Code];
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-09-05 09:23:40

如果我不得不猜测,您触发事件的速度如此之快,以至于它们可能会合并或被跳过,因此,如果您根据调用事件处理程序的次数隐式设置状态,则可能会不同步。 (相反,您应该在事件处理程序内显式确定 UISwitch 的状态(打开或关闭)。)您必须显示更多代码才能确定。至少是 UISwitch 更改状态事件的事件处理程序的代码。

If I had to guess, you're trigging events so fast that they might coalesce or get skipped, so if you're implicitly setting state based on the number of times the event handler is called, you might get out of sync. (Instead you should be explicitly determining the state of the UISwitch -- on or off -- inside the event handler.) You'll have to show more code to be sure. At least the code for the event handler for the UISwitch changed state event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文