rihekopo
rihekopo

Reputation: 3350

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

Upvotes: 0

Views: 196

Answers (3)

nhgrif
nhgrif

Reputation: 62062

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

Upvotes: 2

sebastien FCT
sebastien FCT

Reputation: 630

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

Upvotes: 4

Karim
Karim

Reputation: 736

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Poke" message:[NSString stringWithFormat:@"%@ poked you.", self.senderString] delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alertView show];

Upvotes: 1

Related Questions