【问题标题】:Need Help With Simple Counting Bug in iPhone SDK需要帮助解决 iPhone SDK 中的简单计数错误
【发布时间】:2010-03-20 03:48:52
【问题描述】:

所以我基本上是在制作一个应用程序,将计数添加到一个数字上,然后在您每次点击按钮时显示它。

但是,第一次点击不会执行任何操作,而是在第二次点击时添加一个(按计划)。我已经搜索到地球的尽头寻找没有运气的解决方案,所以我会看看你们能做些什么。 :)

#import "MainView.h"

@implementation MainView

int count = 0;

-(void)awakeFromNib {

    counter.text = @"0";

}

- (IBAction)addUnit {

    if(count >= 999) return;

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
    counter.text = numValue;
    [numValue release];
}

- (IBAction)subtractUnit {

    if(count <= 0) return;

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count--];
    counter.text = numValue;
    [numValue release]; 
}
@end

【问题讨论】:

    标签: iphone xcode iphone-sdk-3.0


    【解决方案1】:

    其实第一次点击是在做某事。

    您正在增加count,因此第一次调用addUnit: count 会增加,但count++ 的返回值是count 的旧值。您想使用++count 进行预增量。

    例子:

    int count = 0;
    int x = count++;
    // x is 0, count is 1
    
    x = ++count;
    // x is 2, count is 2
    

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2021-09-10
      • 2021-12-11
      相关资源
      最近更新 更多