-1

我有 3 个 UILabel 文本字段,我正在尝试比较每个文本字段中的一个值,以突出显示最小值。

我目前有以下代码:

if (((pd1 <= pd3) && (pd1 <= pd2)) || ((pd2 == 0) && (pd3 == 0))){
    pdOne.textColor = [UIColor yellowColor];
}
if (((pd2 > 0) && (pd2 <= pd1)) && ((pd3 >0) && (pd2 <= pd3))) {
    pdTwo.textColor = [UIColor yellowColor];
}
if ((pd3 >0) && (pd3 <=1) && (pd3 <= pd2)) {
    pdThree.textColor = [UIColor yellowColor];
}

在某个地方我会出错,并且希望对此有一些指导。

非常感谢

该操作的完整代码是

- (IBAction)calculateOne:(id)sender; {

NSLog(@"count=%d",count);

pd3 = pd2;
pdThree.text = pdTwo.text;

pdThree.font = [UIFont systemFontOfSize:kDefaultFontSize];
//setup text resizing check here
if (pdThree.contentSize.height > pdThree.frame.size.height) {
    int fontIncrement = 1;
    while (pdThree.contentSize.height > pdThree.frame.size.height) {
        pdThree.font = [UIFont systemFontOfSize:kDefaultFontSize-fontIncrement];
        fontIncrement++;
    }
}

pd2 = pd1;
pdTwo.text = pdOne.text;

pdTwo.font = [UIFont systemFontOfSize:kDefaultFontSize];
//setup text resizing check here
if (pdTwo.contentSize.height > pdTwo.frame.size.height) {
    int fontIncrement = 1;
    while (pdTwo.contentSize.height > pdTwo.frame.size.height) {
        pdTwo.font = [UIFont systemFontOfSize:kDefaultFontSize-fontIncrement];
        fontIncrement++;
    }
}



[brand resignFirstResponder];
[qty resignFirstResponder];
[size resignFirstResponder];
[price resignFirstResponder];
double num1, num2, num3, num4, answerb;

num1 = [qty.text intValue];
num2 = [size.text intValue];
num3 = [price.text intValue];
num4 = (num3/100);
count = (count+1);
NSLog(@"count=%d",count);


answerb=((num3/(num1*num2))*10);
pd1 = answerb;

NSString *answerStringc = [[NSString alloc] initWithFormat:@"%@ %@ x %@ml @ £%.2f = £%.4f/litre ", brand.text, qty.text, size.text, num4, answerb];
pdOne.text= answerStringc;
pdOne.font = [UIFont systemFontOfSize:kDefaultFontSize];
pdOne.textColor = [UIColor whiteColor];
pdTwo.textColor = [UIColor whiteColor];
pdThree.textColor = [UIColor whiteColor];



if (((pd1 <= pd3) && (pd1 <= pd2)) || ((pd2 == 0) && (pd3 == 0))){
    pdOne.textColor = [UIColor yellowColor];
}
if (((pd2 > 0) && (pd2 <= pd1)) && ((pd3 >0) && (pd2 <= pd3))) {
    pdTwo.textColor = [UIColor yellowColor];
}
if ((pd3 >0) && (pd3 <=pd1) && (pd3 <= pd2)) {
    pdThree.textColor = [UIColor yellowColor];
}

//setup text resizing check here
if (pdOne.contentSize.height > pdOne.frame.size.height) {
    int fontIncrement = 1;
    while (pdOne.contentSize.height > pdOne.frame.size.height) {
        pdOne.font = [UIFont systemFontOfSize:kDefaultFontSize-fontIncrement];
        fontIncrement++;
    }
}

NSLog(@"pd1 = %f",pd1);
NSLog(@"pd2 = %f",pd2);

if ((count >1) && ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) && ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]))  {
    self.tweetButton.enabled = YES;
    self.tweetButton.alpha = 1.0f;
    self.facebookButton.enabled = YES;
    self.facebookButton.alpha = 1.0f;

} else if ((count <2)) {
    self.tweetButton.enabled = NO;
    self.tweetButton.alpha = 0.5f;
    self.facebookButton.enabled = NO;
    self.facebookButton.alpha = 0.5f;
}

}

4

2 回答 2

1

我认为这会更容易实现并且更易读(假设你的 pd1、pd2、pd3 是整数):

// Begin with the assumption that pd1 is the smallest
int smallestValue = pd1;
UILabel *smallestLabel = pdOne;

// If pd2 is smaller, use it
if ((pd2 != 0) && (pd2 < smallestValue)) {
     smallestValue = pd2;
     smallestLabel = pdTwo;
}

// If pd3 is smaller, use it
if ((pd3 != 0) && (pd3 < smallestValue)) {
     smallestValue = pd3;
     smallestLabel = pdThree;
}

// Highlight the smallest
smallestLabel.textColor = [UIColor yellowColor];
于 2012-10-08T22:20:42.453 回答
1

我有 3 个 UILabel 文本字段,我正在尝试比较每个文本字段中的一个值,以突出显示最小值。

很简单:假设你的值是浮点数,你可以使用这个:

// You want to ignore values that reads 0, so for each value that is zero we will use CGLOAT_MAX instead in the MIN computation.
// Note: "x?:y" is equivalent to "x?x:y"
// and reads "if x is true (not 0/nil/NO), then use x, else use y"
float minval = MIN( (pd1?:CGFLOAT_MAX) , MIN( (pd2?:CGFLOAT_MAX) , (pd3?:CGFLOAT_MAX) ));

pdOne.textColor   = (pd1 == minval) ? [UIColor yellowColor] : [UIColor blackColor];
pdTwo.textColor   = (pd2 == minval) ? [UIColor yellowColor] : [UIColor blackColor];
pdThree.textColor = (pd3 == minval) ? [UIColor yellowColor] : [UIColor blackColor];

如果您的值是整数,请使用int minval并且更重要的是使用INT_MAX而不是CGFLOAT_MAX.

于 2012-10-08T22:56:37.100 回答