UISwitch快速切换多次
我正在玩我的应用程序,并且我非常快地切换 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我不得不猜测,您触发事件的速度如此之快,以至于它们可能会合并或被跳过,因此,如果您根据调用事件处理程序的次数隐式设置状态,则可能会不同步。 (相反,您应该在事件处理程序内显式确定
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 theUISwitch
changed state event.