UIButton 反复按下

Posted

技术标签:

【中文标题】UIButton 反复按下【英文标题】:UIButton repeated pressing 【发布时间】:2013-09-17 07:09:31 【问题描述】:

我有一个 UITableViewCell 和一个 UIButton。每次我按下按钮时,都会有一个网络调用,它会更新一个标签(增加或减少计数),类似于 Facebook 的“点赞”概念。

问题是当用户反复按下 UIButton 时,值会不断增加或减少。我尝试切换 userInteraction 并设置 setEnabled 状态。还是不行。

然后我尝试按照link 的建议使用块。还是行不通。我对块很陌生。我在这里做错了吗?

这是没有块的实现:

    - (void) giveKarmaCheck:(BOOL)complete // network delegate after the update
    
        NSLog(@"Completed!");
        _karmaBeingGiven = NO;
        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section]];
        int qKarma = [[tempDict objectForKey:@"qkarma"] integerValue];
        NSString *karmaCountString;
        QXTHomeCell *questionCell = (QXTHomeCell*)[self.questionsTable cellForRowAtIndexPath:_indexPath];
        if (_karmaGiven)
        
            [tempDict setValue:[NSNumber numberWithInt:1] forKey:@"qkarmaStatus"];
            qKarma++;
            [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-black"]];
            questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue + 1];

            // Question Owner Karma
            NSMutableString *karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]+1];

            [karmaLabelText appendString:karmaCountString];
            questionCell.karmaLabel.text = karmaLabelText;
        
        else
        
            [tempDict setValue:[NSNumber numberWithInt:0] forKey:@"qkarmaStatus"];
            qKarma--;
            [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-grey"]];
            questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue - 1];

            // Question Owner Karma
            NSMutableString *karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]-1];

            [karmaLabelText appendString:karmaCountString];
            questionCell.karmaLabel.text = karmaLabelText;
        
        NSLog(@"Count *** %@", karmaCountString);

        NSMutableArray *tempAnswerArray = [[NSMutableArray alloc] initWithArray:[_questions objectForKey:@"questions"]];
        NSMutableDictionary *tempAnswerDict = [[NSMutableDictionary alloc] initWithDictionary:[tempAnswerArray objectAtIndex:_indexPath.section]];

        [tempAnswerDict setValue:karmaCountString forKey:@"qownerkarma"];
        [tempAnswerArray replaceObjectAtIndex:_indexPath.section withObject:tempAnswerDict];
    //    NSLog(@"Array %@", tempAnswerDict);
        [_questions setObject:tempAnswerArray forKey:@"questions"];

        NSLog(@"Total %@ Count %d", [[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section], [[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] count]);



        [tempDict setValue:[NSNumber numberWithInt:qKarma] forKey:@"qkarma"];
        [[_questions mutableArrayValueForKey:@"questions"] replaceObjectAtIndex:_indexPath.section withObject:tempDict];

        NSMutableString *withoutCount = [[NSMutableString alloc] initWithString:[QXTUtility removeLastWord:questionCell.karmaButton.titleLabel.text]];
        [withoutCount appendString:[NSString stringWithFormat:@" %d", questionCell.karmaCount.integerValue]];
        [questionCell.karmaButton setTitle:[QXTUtility stripDoubleSpaceFrom:withoutCount] forState:UIControlStateNormal];

        [questionCell.karmaButton setUserInteractionEnabled:YES];

    

对块实现做了同样的事情:

- (void) giveKarmaCheck:(BOOL)complete

    QXTHomeCell *questionCell = (QXTHomeCell*)[self.questionsTable cellForRowAtIndexPath:_indexPath];
    [questionCell.karmaButton setEnabled:NO];

    __block NSMutableString *karmaLabelText, *withoutCount;

    dispatch_async(dispatch_get_global_queue(0,0), ^
        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section]];
        int qKarma = [[tempDict objectForKey:@"qkarma"] integerValue];
        NSString *karmaCountString;
        if (_karmaGiven)
        
            [tempDict setValue:[NSNumber numberWithInt:1] forKey:@"qkarmaStatus"];
            qKarma++;

            // Question Owner Karma
            karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]+1];

            [karmaLabelText appendString:karmaCountString];

        
        else
        
            [tempDict setValue:[NSNumber numberWithInt:0] forKey:@"qkarmaStatus"];
            qKarma--;

            // Question Owner Karma
            NSMutableString *karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]-1];

            [karmaLabelText appendString:karmaCountString];
        

        NSMutableArray *tempAnswerArray = [[NSMutableArray alloc] initWithArray:[_questions objectForKey:@"questions"]];
        NSMutableDictionary *tempAnswerDict = [[NSMutableDictionary alloc] initWithDictionary:[tempAnswerArray objectAtIndex:_indexPath.section]];

        [tempAnswerDict setValue:karmaCountString forKey:@"qownerkarma"];
        [tempAnswerArray replaceObjectAtIndex:_indexPath.section withObject:tempAnswerDict];
        //    NSLog(@"Array %@", tempAnswerDict);
        [_questions setObject:tempAnswerArray forKey:@"questions"];

        NSLog(@"Total %@ Count %d", [[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section], [[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] count]);



        [tempDict setValue:[NSNumber numberWithInt:qKarma] forKey:@"qkarma"];
        [[_questions mutableArrayValueForKey:@"questions"] replaceObjectAtIndex:_indexPath.section withObject:tempDict];

        withoutCount = [[NSMutableString alloc] initWithString:[QXTUtility removeLastWord:questionCell.karmaButton.titleLabel.text]];
        [withoutCount appendString:[NSString stringWithFormat:@" %d", questionCell.karmaCount.integerValue]];

        dispatch_async(dispatch_get_main_queue(), ^
            [questionCell.karmaButton setEnabled:YES];
            if (_karmaGiven)
            
                [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-black"]];
                questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue + 1];
                questionCell.karmaLabel.text = karmaLabelText;
            
            else
            
                [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-grey"]];
                questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue - 1];

                questionCell.karmaLabel.text = karmaLabelText;
            
            [questionCell.karmaButton setTitle:[QXTUtility stripDoubleSpaceFrom:withoutCount] forState:UIControlStateNormal];
        );


    );

    NSLog(@"Completed!");


【问题讨论】:

等等,所以你是说多次按下按钮会导致多个动作。这就是按钮应该如何工作的定义。您是否在寻求一种禁用 UIButton 的方法?您可以为此使用 setEnabled 为 NO。这个问题有点不清楚。 尝试禁用 self.view 并检查。并确保在主线程上更新 UI。 @Bergasms 我尝试将 setEnabled 设置为 NO,但这没有帮助。 UIButton 的“启用”状态会在几分之一秒左右的时间内变为 YES。当用户不断点击 UI 时,会导致递增/递减 @Prasad_R 禁用 self.view 会导致我的 UI 冻结。在此期间,用户也应该能够点击单元格。 会不会容易很多?您将分离您的关注点(向上/向下业力),并且您可以轻松地为两个不同的功能使用不同的图像/文本。 【参考方案1】:

禁用按钮不起作用的原因是因为它是对 UIKit 对象的调用,应该在主线程上执行。基本上,您不知道从哪个线程调用网络委托方法(可能是后台)。在委托方法中,与 UI 有关的任何内容都应发送到主队列。方法中:

- (void) giveKarmaCheck:(BOOL)complete;

封装 UI 调用

dispatch_async(dispatch_get_main_queue(), ^
    // Do UI stuff on this thread
);

Note主队列和全局队列不一样。

【讨论】:

以上是关于UIButton 反复按下的主要内容,如果未能解决你的问题,请参考以下文章

如何在按下其他 UIButton 时触发 UIButton 的“按下效果”或“突出显示效果”

如何检测 UIButton 按下状态? [关闭]

UISegmentedView 中的 UIButton 没有被按下

按下一个 UIButton 会触发另一个 UIButton:我不希望这种情况发生

按下时如何获取 UIButton 的标题

UIButton 按下时移动