iphone - UIButton 反复按下

标签 iphone ios objective-c uibutton objective-c-blocks

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

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

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

这是没有 block 的实现:

    - (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];

    }

对 block 实现做了同样的事情:
- (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!");

}

最佳答案

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

- (void) giveKarmaCheck:(BOOL)complete;

包装 UI 调用
dispatch_async(dispatch_get_main_queue(), ^{
    // Do UI stuff on this thread
});

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

关于iphone - UIButton 反复按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843435/

相关文章:

ios - Autolayout layoutIfNeeded 正在打破约束

ios - 如何使用 iOS 将文件上传到 Amazon S3 并获取 url 作为响应

ios - 核心数据: fetching from related entity

ios - 在 View Controller 之间传递数据

ios - 如何在 Swift 中使用按钮和 if/else 停止背景音乐?

objective-c - 带静态轴的核心图

ios - 在 UIView Controller 和 UITableViewController 之间传递值

iphone - 为什么我会收到此 "libxml/tree.h file not found"错误?

ios - 缩放 UIViewController 和 child 以适应 iPad 或 iPhone 屏幕

iphone - 哪里可以下载旧的 iPhone SDK?