views:

193

answers:

3

I have a sting that is equal to a text field that i want to insert inside of a UIAlertView.

NSString *finalScore = [[NSString alloc] initWithFormat:[counter2 text]];

UIAlertView *myAlertView = [[UIAlertView alloc]
                                initWithTitle:finalScore
                                message:@"You scored X points"
                                delegate:self
                                cancelButtonTitle:nil
                                otherButtonTitles:@"OK", nil];

[myAlertView show];
[myAlertView release];

I want to replace"X" with the score from the string "finalScore"

I know how to have it just with the score, you would simple just enter the strings name without quotes under the message section, but i want the the text there too along with the string.

Thanks a bunch, Chase

A: 

Instead of:

@"You scored X points"

use:

[NSString stringWithFormat:@"You scored %@ points", finalScore]
pix0r
A: 

Replace your message: parameter with:

message:[NSString stringWithFormat:@"You scored %@ points", finalScore]

Also, note that your original code for finalScore uses string formatting without any arguments, which is either a no-op or unsafe (if it contains a % symbol). You are also leaking your finalScore object.

Jason
+1  A: 

Try something like the following, and pass it as the message variable to UIAlertView's initialization:

NSString* message = [NSString stringWithFormat:@"You scored %@ points",
                              [counter2 text]];

You might have to change some details about the formatting string depending on the type that comes out of [counter2 text], however, though the above should work in many of the cases you'll encounter.

fbrereto