In my Calorie Tracker App, I have two textfields. One is for the name of the food, and the other is for the amount of calories.To check and see if they are filled I wrote the following code:
if([foodString isEqual: #" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not entered the name of the meal!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([self.amountText isEqual: #" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not enteted the amount of calories!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([self.amountText isEqual:#" "] && [foodString isEqual:#" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You haven't entered anything!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
So for the one where the amountText textfield isn't filled out, but the nameOfFood is filled out, there is an alert. For the rest however, there is no alert. If anyone could provide the proper syntax for checking to see if my textfields are being filled out properly, it would be much appreciated.
Remember, my first textfield is a string value, and the other is an integer value.
Thanks in advance.
Related
am getting following response from web service and i need to fetch the string
Error: no record found in database.
shown below:
d = (
"Error: no record found in database."
);
here is my code
if([[[[dictiona objectForKey:#"d"] objectForKey:#""] objectAtIndex:0] isKindOfClass:[NSString class]] == YES)
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"No Record Found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[myAlertView show];
}
Your code should not check for a second objectForKey, as there isn't a second NSDictionary to check for, because your second item is an NSArray, which doesn't respond by keys. This should work for you instead:
if ([[[dictionary objectForKey:#"d"] objectAtIndex:0] isKindOfClass:[NSString class]])
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"No Record Found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[myAlertView show];
}
Fresh and green newbie here. I just started programming and already got stuck with UIAlert. I've searched on this site but there are so many postings and as I said I am kinda new to all this so I don't know what to really look for. This is what I have.
-(void)showError {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please try again later. Have a nice day."
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
}
Need to do
[alert show];
to present
Code should be :
-(void)showError {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please try again later. Have a nice day."
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[alert show];
I always forget this too. Happy Coding.
-(void)showError
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please try again later. Have a nice day."
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
}
i have create two UIAlertView views in one method. Code like below
-(void) alert{
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
}
after call this method. iPhone app will appear popup for 2 times.
first appear is alert_1, disappear alert_1 and appear alert_2
after user press ok button in alert_2 thn appear alert_1
should be remove alert_1 when appear alert_2
is possible to remove previous alert view?
Send message - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated to alert1.
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
[alert_1 dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
I am not clear about what your requirement is. But from what I understood you want alert_2 to popup first and when clicked on the "OK" button you want to dismiss that alert view and popup alert_1
- (void) alertview
{
alert_1 = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert_1 setTag:1];
alert_1.delegate = self;
alert_2 = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 setTag:2];
alert_2.delegate = self;
[alert_2 show];
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==2)
{
[alert_1 show];
}
}
Please note to declare your alert views in your .h file
Get UIAlertView by its tag or #Property and use this [myAlertView dismissWithClickedButtonIndex:-1 animated:YES];
I've written a simple app which validates user input (whether NULL or longer than a define length). It should return validation error messages when validation fails and otherwise, redirect to another page.
However, the app only returns the messge for the first condition (Username is Empty) for all scenarions. (Such as username is filled and password is empty, etc.)
m file:
- (IBAction)doLogin {
if(uname.text==NULL) {
UIAlertView *err1 = [[UIAlertView alloc]
initWithTitle:#"Required field!" message:#"Username is empty." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err1 show];
NSLog(#"%#",uname.text);
}
else if(passw.text==NULL) {
UIAlertView *err2 = [[UIAlertView alloc]
initWithTitle:#"Required field!" message:#"Password is empty." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err2 show];
NSLog(#"%#",passw.text);
}
else if (uname.text.length < 6)
{
UIAlertView *err3 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a username longer than 6 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err3 show];
NSLog(#"%#",uname.text);
}
else if (uname.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",uname.text);
}
else {
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Thank you" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"OK", nil];
[alert show];*/
UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:#"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];
}
An alternative to karthika (but using similar structure) this will provide feedback on the entire form in a single message. Perhaps a little more user friendly and certainly reduces negative user interaction.
-(BOOL)isFormDataValid{
NSMutableArray *errorMessages = [[NSMutableArray alloc] init];
if([self.emailTextField.text isEqualToString:#""])
{
[errorMessages addObject:NSLocalizedString(#"Please enter email",nil)];
}
if([self.passwordTextField.text isEqualToString:#""])
{
[errorMessages addObject:NSLocalizedString(#"Please enter password",nil)];
}
if ([errorMessages count]) {
NSString * msgs = [errorMessages componentsJoinedByString:#"\n"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Whoops!",nil) message:msgs delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return NO;
} else {
return YES;
}
}
-(BOOL)isFormDataValid{
NSString *errorMessage = nil;
UITextField *errorField;
if([nameTextField.text isEqualToString:#""])
{
errorMessage = #"Please enter username";
errorField = nameTextField;
}
else if([[nameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{
errorMessage = #"white spaces not allowed";
errorField = nameTextField;
}
else if([passwordTextField.text isEqualToString:#""])
{
errorMessage = #"Please enter password";
errorField = passwordTextField;
}
else if([[passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{
errorMessage = #"white spaces not allowed";
errorField = passwordTextField;
}
if (errorMessage) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed!" message:errorMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[errorField becomeFirstResponder];
return NO;
}else{
return YES;
}
}
Apart from the fact that you do
else if (uname.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",uname.text);
}
instead of
else if (passw.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",passw.text);
}
Your code should work just fine.
Also, bear in mind that a text field's text won't be nil, it will just be an empty string (lenght == 0), unless you explicitly set it to nil.
Hello to everyone i would like to place my thinking about an alertview. I am thinking to create an Uialertview which will prompt the user to input two integers. Then i would like to retrieve these two integers and put the one on a timer and the second in an sql statement. So if anyone can help on how to implement that i would apreciate it. Thank you all.
Here is my code until now
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0] text]);
[alertView show];
You need to use delegate methods of UIAlterView.
First of all make "self a delegate" instead of nil.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
Than add this function to your class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
NSLog(#"%#", field.text);
}
To be 100% correct add also in your header class information that you implement this protocol .
// Try this
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView textFieldAtIndex:0].delegate = self;
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0]text]);
}