我有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;
}
}
答案 0 :(得分: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];
答案 1 :(得分: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
。