-1

3 つの UILabel Textfields があり、それぞれの値を比較して、最小値を強調表示しようとしています。

現在、次のコードがあります。

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

3 つの UILabel Textfields があり、それぞれの値を比較して最小値を強調表示しようとしています。

簡単です。値が浮動小数点数であると仮定すると、次のように使用できます。

// 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_MAXCGFLOAT_MAX

于 2012-10-08T22:56:37.100 に答える
1

これはより簡単に達成でき、次のように読みやすくなると思います(pd1、pd2、pd3がintであると仮定します):

// 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 に答える