I am trying to detect whether a NSNumber is between 0 and 255 or not. Whenever I run the app, I receive the alert view that my number is greater than 255, even when it is not. I do not have this problem with 0.
if (redValue < 0) {
NSLog(#"Red value is less than 0");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Your number must be greater than 0." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
} else if (redValue > 255) {
NSLog(#"Red value is greater than 255");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Your number must be less than 255." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
Additionally, I receive this warning on the "else if (redValue > 255)" line: Ordered comparison between pointed and integer ('NSNumber *' and 'int'), So I'm assuming I have to convert this NSNumber to an integer?
Should be:
if([redValue intValue] < 0) {
...
if([redValue intValue] > 255) {
...
Assuming it is an int. If it isn't go to the NSNumber Class Reference look under "Accessing Numeric Values" and replace intValue with the appropriate thing.
use intValue to get the number as an int:
[redValue intValue]
try this
[redValue intValue] > 255
if (redValue.intValue >255)
{
// it's greater than 255
}
Related
I want to check if the text inserted in a textfield is also in my NSMutableArray.
So let's say my NSMutableArray has these objects: "Hey, Hello, No, Yes".
Then when a user enters the text: "Hello" i want there to appear a UIAlertView. I now have the following:
for (int slt = 0; slt < [zouten count]; slt++) {
if (zout.text = [zouten objectAtIndex:slt]) {
alert = [[UIAlertView alloc]initWithTitle:#"Goedzo!" message:[NSString stringWithFormat:#"Je hebt een zout gevonden"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
}
[alert show];
But somehow the message appears with every word. what am i doing wrong?
When you compare like this:
if (zout.text = [zouten objectAtIndex:slt])
you are actually assigning instead of comparing so it will be TRUE always.Therefore instead of using =, you should compare like this:
if ([zout.text isEqualToString:[zouten objectAtIndex:slt]])
Your code should be:
for (int slt = 0; slt < [zouten count]; slt++) {
if ([zout.text isEqualToString:[zouten objectAtIndex:slt]]) {
alert = [[UIAlertView alloc]initWithTitle:#"Goedzo!" message:[NSString stringWithFormat:#"Je hebt een zout gevonden"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
}
You can use isEqualToString method to compare strings.
if([zout.text isEqualToString:[zouten objectAtIndex:slt]])
Alternatively you could make your code more concise by converting to a set and checking for the entry, e.g.
NSSet *set = [NSSet setWithArray: zouten];
if([set containsObject:zout.text]) {
...
}
I am having trouble getting a double to show up in my alert view. The string keeps showing up as 0 in the AlertView. Thanks in advance!
double binRadius = [txtRadiusInches.text doubleValue] / 12 + [txtRadiusFeet.text doubleValue];
NSString *myString = [NSString stringWithFormat:#"%d", binRadius];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Estimatated Amount"
message:myString
delegate:self
cancelButtonTitle:#"Continue"
otherButtonTitles:#"Clear All", nil];
[alert show];
%d (and %i) is the format specifier for integers (decimals). For floating point numbers you should use %f.
All I want to do, If there is nothing on UITextField , application will show an alert. I don't understanding what's wrong I'm doing. here is my code:
- (IBAction)Calculate:(id)sender {
/*If there is nothing on textfield, it will show an alert*/
if ([addtake.text length] && [Subtake.text length] == 0)
{
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:#"Alert"message:#"Empty TextField not allowed" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles: nil];
[myAlert show];
}
else{
NSString *Adding =[addtake text];
NSInteger number = [Adding intValue]; /*Converting String to int*/
NSInteger new = 5 + number;
NSString *newString = [NSString stringWithFormat:#"%d",new];
[myAdd setText:newString];
/*Subtruct*/
NSString *Subtruct =[Subtake text];
NSInteger SubNumber = [Subtruct intValue];
NSInteger newSub = 10 - SubNumber;
NSString *newSubString = [NSString stringWithFormat:#"%d",newSub];
[mySub setText:newSubString];
[addtake resignFirstResponder];
[Subtake resignFirstResponder];
}
}
Try making each sub-clause in your if statement explicit - if only for readability...
I would use logical OR, not logical AND in your test. So that if either text field is empty the alert would show
if ((![addtake.text length]) || (![Subtake.text length]))
{
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:#"Alert"message:#"Empty TextField not allowed" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles: nil];
[myAlert show];
}
if ([addtake.text length] == 0 && [Subtake.text length] == 0)
I assume that addText is your tesxField so you need to set the condition [addtake.text length] == 0 aswell.
I want a UIAlertView to popup if the user doesn't have enough points to purchase an upgrade. So far I have this code to spend points to get the upgrade. If the upgrade costs 300 points, and the user only has 150, I would like the UIAlertView to calculate the amount needed and say something like, "Sorry, you need 150 more points to buy this upgrade". Here's my code. ('mag' and 'score' are both ints.)
-(int)mag {
return [[NSUserDefaults standardUserDefaults] integerForKey:kMagDefaultsKey];
}
-(int)score {
return [[NSUserDefaults standardUserDefaults] integerForKey:kScoreDefaultsKey];
}
-(void)setMag:(int)value {
[[NSUserDefaults standardUserDefaults] setInteger:value forKey:kMagDefaultsKey];
}
-(void)setScore:(int)value {
[[NSUserDefaults standardUserDefaults] setInteger:value forKey:kScoreDefaultsKey];
}
-(void)plusFiveMag:(id)sender {
self.mag = self.mag + 5;
self.score = self.score - 300;
}
Try somethin like this...
-(void)alertview
{
if(self.Mag>self.cost)
{
int difference;
difference=self.Mag-self.cost;
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:#"your message" message:differene delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[Alert show];
[Alert release];
}
}
I'm sick of writing basic UIAlertView's, ie:
UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc
Instead of doing this, is it possible to put all this in a "helper" function, where I can return the buttonIndex, or whatever an alert usually returns?
For a simple helper function I guess you could feed parameters for the title, message, I'm not sure whether you can pass delegates in a parameter though, or bundle info.
In pseudo-code, it could be like this:
someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc
Any help on this would be great.
Thanks
In 4.0+ you can simplify the alert code using blocks, a bit like this:
CCAlertView *alert = [[CCAlertView alloc]
initWithTitle:#"Test Alert"
message:#"See if the thing works."];
[alert addButtonWithTitle:#"Foo" block:^{ NSLog(#"Foo"); }];
[alert addButtonWithTitle:#"Bar" block:^{ NSLog(#"Bar"); }];
[alert addButtonWithTitle:#"Cancel" block:NULL];
[alert show];
See Lambda Alert on GitHub.
This is what I wrote, when I got sick of doing the same:
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName {
[self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: nil];
}
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName informing:(id)delegate {
[self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: delegate];
}
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName withExtraButtons:(NSArray *)otherButtonTitles informing:(id)delegate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate: delegate
cancelButtonTitle: firstButtonName
otherButtonTitles: nil];
if (otherButtonTitles != nil) {
for (int i = 0; i < [otherButtonTitles count]; i++) {
[alert addButtonWithTitle: (NSString *)[otherButtonTitles objectAtIndex: i]];
}
}
[alert show];
[alert release];
}
You can't write a function that will display an alert and then return a value like a buttonIndex though, because that value-returning only occurs when the user presses a button and your delegate does something.
In other words, the process of asking a question with the UIAlertView is an asynchronous one.