More then one Localized string is not allowing in UIAlertView - ios

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.

Related

iOS ARC UIAlertView leaking memory

I have simple method showing AlertView with textfield. Instruments showing memory leak in this. Please explain.
- (void)method {
NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:#"key"];
if (value == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alertView.tag = 101;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *txtGroup = [alertView textFieldAtIndex:0];
[txtGroup becomeFirstResponder];
[alertView show];
alertView = nil;
}
}
Please find the screenshot of Instruments:
You need to create alertView as :
static UIAlertView *alertView = nil;
if (!alertView){
alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}

Unable to pass a variable into alert view

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

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

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.

Writetourl does not work

I have tried for a week to get writeToUrl to work, but no... nothing.
I simply want to update a file on my server from an iPhone app.
This is the code:
NSURL *url = [NSURL URLWithString:#"http://user:pw#192.168.120.167/test.txt"];
NSString *teststring = #"it works";
if ([teststring writeToURL:url atomically:YES encoding:NSASCIIStringEncoding error:nil]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Status" message: #"Yessss" delegate: self cancelButtonTitle: #"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Status" message: #"Noooooo" delegate: self cancelButtonTitle: #"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
I switched from writeToURL to writeToFile, using the exact same file path, and it suddenly worked. Seems like there might be a bug in writeToURL. This was tested on iOS 7.1.
You can only write to local files. See documentation:
Since at present only file:// URLs are supported, there is no difference between this method and writeToFile:options:error:, except for the type of the first argument.

Resources