Help adding string inside alert view - iphone

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

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.

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.

Instead of:
#"You scored X points"
use:
[NSString stringWithFormat:#"You scored %# points", finalScore]

Related

Can't figure out an error with stringWithFormat

I'm having a really basic problem with NSString stringWithFormat. I want to take the name that the user enters and display in an alertView: Welcome username.
NSString *welcomeMessage = [NSString stringWithFormat:(#"Welcome %#", passedData)];
UIAlertView *alert = [[UIAlertView alloc] //show alert box with option to play or exit
initWithTitle: welcomeMessage
message:#"Once you press \"Play\" the timer will start. Good luck!"
delegate:self
cancelButtonTitle:#"I want out!"
otherButtonTitles:#"Play",nil];
[alert show];
passedData is the username that has been entered. The way I have it at the moment - only the username is being displayed in the title of the alert box, and not the "Welcome" part. I know i'm missing some really basic knowledge here but would appreciate some help.
I think that () are not needed. Try using that:
NSString *welcomeMessage = [NSString stringWithFormat:#"Welcome %#", passedData];
instead of
NSString *welcomeMessage = [NSString stringWithFormat:(#"Welcome %#", passedData)];
Hope it helps

iPhone: Using Alerts to Help Debugging

I've been building a rather complex system and there's come the time now where I want more concise debugging. I would like to display the contents of a variable (for this example an NSString called v_string) in a notification window (the kind of window that appear when you receive an SMS text).
Is there an easy way to just call an alert with a variable?
Thanks in Advance,
Dan
NSLog does not do? If not (like if you need to debug an application running on a disconnected device), you can extend the UIAlertView with a category:
#implementation UIAlertView (Logging)
+ (void) log: (id <NSObject>) anObject
{
NSString *message = [anObject description];
UIAlertView *alert = [[self alloc] initWith…];
[alert show];
[alert release];
}
And then in code:
NSString *anInterestingString = …;
[UIAlertView log:anInterestingString];
When you build the string to display in the alert window, simply append your variable's string represenation using stringByAppendingString.
Alert window is cumbersome. Use NSLog instead:
NSLog(#"Variable is: %#", v_string);
And in Xcode's console you will see that text.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"My Debug String" message:v_string delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[message show];
[message release];
I think this way you can see what you want.
But, as zoul said, why not to use NSLog(#"my var: %#", v_string); ?
Hope that it helps.

How do I dynamically create an NSString object based on the number of objects in an array?

Members of my favorite forum, I am stuck with a simple problem (chalk it up to the fact that I am fairly new)...
I am using an action sheet. I don't know how many buttons to display, so I decided to set the action sheet delegate to display FOUR buttons in total using string variables, knowing that if I only need to display 2, I can set the third variable to NIL and achieve that objective.
Example:
NSString *firstVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:0]];
NSString *secondVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:1]];
NSString *thirdVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:2]];
NSString *fourthVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:3]];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Please select a vehicle:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:firstVehicle, secondVehicle, thirdVehicle, fourthVehicle, nil];
NSLog(#"firstOtherButtonIndex in the List is %i", [actionSheet firstOtherButtonIndex]);
[actionSheet showInView:self.view];
[actionSheet release];
break;
With that said, I don't always know that i will have four strings to create...when I pull back the myVehicleList array, I can use the count method to determine how many variables I have.
Question: So how does a smart programmer actually solve this problem? Meaning, how do I cycle through a for statement perhaps using the count returned when I check the array to only establish a certain number of strings and set the next string to nil?
Does that make sense? I can do IF statements for days, right. If count = 4, then set all four strings. But if count is 3, I set three strings to the array object values and the fourth to nil. But if the count is 2, I set two strings to the array object values and the third to nil, and so on.
There has to be a more effective way to dynamically create strings? I guess I could use fast enumeration, but how do I dynamically create a unique string object...
I hope I make some sense.
thanks!
NSMutableArray *vehicles = [[NSMutableArray alloc] init];
[vehicles addObject:#"One"];
[vehicles addObject:#"Two"];
[vehicles addObject:#"Three"];
[vehicles addObject:#"..."];
[vehicles addObject:#"Ten"];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Please select a vehicle:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *vehicle in vehicles) {
[actionSheet addButtonWithTitle:vehicle];
}
[actionSheet showInView:self.view];
[actionSheet release];
I will prefer using an array of strings.
The real problem you're up against is that there's no good way to convert between a dynamic array and a varargs parameter (that is, one where you can pass any number of variables as arguments). If you wanted to do it the looping way and still use the varargs parameter, the best approach would be to create an array like NSString *strings[4] = {nil}, loop through it and then pass strings[0], strings[1], strings[2], strings[3] as arguments.
But I wouldn't do that. It's really duct-tapey. I would create the sheet with only the cancel button, then loop through an NSArray of strings that you want to use as titles and [sheet addButtonWithTitle:title].

UIAlertView Messages

Trying to include a instance variable in a message that a UIAlertView Shows.
lostAlert = [[UIAlertView alloc] initWithTitle:#"Sorry" message:(#"You Were Wrong, the correct structure was %#", structureName) delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
however, when the Alert is shown, no message is shown.
Any ideas and help would be appreciated :)
Sam
did you try it with:
[NSString stringWithFormat:#"You Were Wrong, the correct structure was %#", structureName]
instead of
(#"You Were Wrong, the correct structure was %#", structureName)

UITextView hasText not working?

I am assigning the contents of the clipboard to the UITextView text property. However, when I check the hasText property, the condition is always false.
NSString paste_text = [[NSString alloc] init];
self.paste_text = [UIPasteboard generalPasteboard].string;
....
my_UITextView.text = self.paste_text;
//THIS CONDITION IS ALWAYS FALSE
if (my_UITextView hasText)
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Text ready to copy"
message:err_msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
You need to send your UITextView the hasText message by using brackets:
if ([my_UITextView hasText])
UPDATE:
Do you know that your UTTextView has text? You might want to check it on the console:
my_UITextView.text = self.paste_text;
NSLog(#"my_UITextView.text = %#",my_UITextView.text); // check for text
//THIS CONDITION IS ALWAYS FALSE
if ([my_UITextView hasText])
First off, your paste_text isn't used as intended since right after you alloc, it will immediately be discarded, possibly released, maybe not. You could actually just do away with the [[NSString alloc] init];.
Then add the following to test your code:
// delete:
// NSString paste_text = [[NSString alloc] init];
NSLog([UIPasteboard generalPasteboard].string);
self.paste_text = [UIPasteboard generalPasteboard].string;
NSLog(self.paste_text);
....
my_UITextView.text = self.paste_text;
NSLog(#"my_UITextView is %#, text contained: %#, my_UITextView , my_UITextView.text);
The first NSLog prints out the pasteboard string, the second the string once it is passed on to your paste_text, and the last will let you know if my_UITextView is non-nil, and what text it contains.
Also, if paste_text is a #property, what are its attributes? The text from [UIPasteboard generalPasteboard].string needs to be copied into it, otherwise when the pasteboard's string is changed, so is your paste_text.
How are you creating the my_UITextView property? If you did it in InterfaceBuilder, it's possible that you forgot to create an IBOutlet.