Unable to pass a variable into alert view - ios

I have an array of names in 'names' . I am trying to display an alert when the user taps on a table cell. If the user touches the first cell then ,[names objectatindex:0] should be the message in the alert view. Here is what I have done
UIAlertView *okAlert = [[UIAlertView alloc] initWithTitle:#"ok" message:#"You have just tapped [names objectatindex:0]" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
okAlert.tag=0;
[okAlert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
In the message it is not displaying the name. I couldn't figure out the problem. Could you please help me in this?
Thanks in advance.

Try this:
UIAlertView *okAlert = [[UIAlertView alloc] initWithTitle:#"OK"
message:[NSString stringWithFormat:#"You have just tapped %#.", [names objectAtIndex:0]]
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];

message:#"You have just tapped [names objectatindex:0]"
You've hidden that code inside a string constant. Now it's just letters.
Try:
UIAlertView *okAlert = [[UIAlertView alloc] initWithTitle:#"ok"
message:[#"You have just tapped " stringByAppendingString:names.firstObject]
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];

Related

More then one Localized string is not allowing in UIAlertView

I tried to do localization. It's Working fine, but here, I am facing a problem in UIAlertView. It's not working for more then one string, but I need to keep more then one string AlertView OtherButton Titles.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"AlertTitle", #"")
message:NSLocalizedString(#"AlertMessage", #"")
delegate:nil
cancelButtonTitle:NSLocalizedString(#"AlertCancel", #"")
otherButtonTitles:NSLocalizedString(#"AlertOk", #""), nil];
Try following piece of code.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"AlertTitle", nil) message:NSLocalizedString(#"AlertMessage", nil) delegate:nil cancelButtonTitle:NSLocalizedString(#"Cancel", nil) otherButtonTitles:NSLocalizedString(#"Change Password", nil), NSLocalizedString(#"Profile", nil), NSLocalizedString(#"log out", nil), nil];
[alertView show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertTitle",nil)] message:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertMessage",nil)] delegate:nil cancelButtonTitle:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertCancel",nil)] otherButtonTitles:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertOk",nil)], nil];
To simplify all NSLocalizedString calls, I use this technic
#define Trad(string, ...) [NSString stringWithFormat: NSLocalizedString(string, nil), ##__VA_ARGS__]
Then you can create your AlertView like this:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Trad(#"Title")
message:Trad(#"Message")
delegate:nil
cancelButtonTitle:Trad(#"Cancel")
otherButtonTitles:Trad(#"Button1"),
Trad(#"Button2"),
Trad(#"Button3"), nil];
The #define statement isn't necessary, it's only purpose is to simplify the code.

Passing NSString into UIAlertview's message content

I would like to display something like this: SampleUser poked you. in a UIAlertView's message, but actually i'm getting errors. I know how to do it with a simple string, i don't know how to do it with a string that contains another string.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Poke" message:#"%# poked you.", self.senderString delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
[alertView show];
You should create your composed-NSString first and then call it in your UIAlertView:
NSString *message = [NSString stringWithFormat:#"%# poked you.", userName];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Poke" message:message, self.senderString delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
[alertView show];
The problem here is that for the message: argument, you're attempting to send this:
#"%# poked you.", userName
This doesn't make any sense.
Instead, you need to send an NSString object as the argument.
NSString *message = [NSString stringWithFormat:#"%# poked you.", self.senderString];
Now that we've created an NSString object, we can use this object as the message argument.
You could create this object embedded in the call to create the alert view, but for readability and debugging , it's better to do it this way.
NSString *message = [NSString stringWithFormat:#"%# poked you.", self.senderString];
UIAlertView *pokeAlert = [[UIAlertView alloc] initWithTitle:#"Poke"
message:message
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
[pokeAlert show];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Poke" message:[NSString stringWithFormat:#"%# poked you.", self.senderString] delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
[alertView show];

is possible to remove previous alert view?

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];

Checking to see if textfields are filled out

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.

Alertview with integer

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]);
}

Resources