Passing NSString into UIAlertview's message content - ios

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

Related

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

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.

How to access a string from dictionary

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

How to call NSString from json to UIAlertView?

I would like to pass some values from json to a message of UIAlertView. I have some codes
- (void)jsonParse{
NSString* path = #"http://phdprototype.tk/getResultData.php";
NSURL* url = [NSURL URLWithString:path];
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSDictionary* resultDic = [dic objectForKey:#"maxid"];
NSString* recData = [resultDic objectForKey:#"recommendData"];
NSString* rData = [resultDic objectForKey:#"room"];
NSString* lData = [resultDic objectForKey:#"level"];
}
- (void)locationView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#."]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
[alert show];
}
As I know, I have to do something in
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#."]. However, I have no idea how to do it. Can someone tell me how to pass the values of lData and rData to the message of uialertview??
You can make a global NSString variable which defines you message
In .h file
NSString *message;
In jsonParse method
- (void)jsonParse {
//Your Stuff
message = [NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#", lData, rData];
}
and then in your UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Recommendation"
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"GO", nil];
[alert show];
- (void)jsonParse{
//your code
[self locationViewWithRoom:rData level:lData];
}
- (void)locationViewWithRoom:(NSString *)room level:(NSString *)level
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.", room, level]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
[alert show];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.", iData, rData]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
Just write the values after you write the argument for stringWithFormat. The number of your arguments should match with the number of %#s used in argument string, and your arguments should be comma separated, otherwise you will get a build error.
Declare both the variables (rData & lData) globally (means, in your interface) and then use it like this
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.",lData,rData]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
Its actually pretty easy. You are doing right just few things are missing.
Declare rData & lData as properties in your class,
#interface <classname>
#propert (nonatomic,strong) NSString *lData;
#propert (nonatomic,strong) NSString *rData;
#end
Then your string can be formed using,
[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.", lData, rData];
This should resolve your issue.

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