Pregunta

I want a UIAlertView to warn the user if there are no items matching his/her chosen search criteria. My initial idea was to use this code:

if (aOiCount == 0)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No instances of %@",self.thisSpec.activityOfInterest message:@"Please select an activity or Cancel" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

The idea being to slip an actual activity name into the title, like in an NSLog string.

Unfortunately, this doesn't work. Compiler tells me Expected ":"

Is it possible to use a variable like this, and if so, how?

Thanks!

¿Fue útil?

Solución

call this line

@"No instances of %@",self.thisSpec.activityOfInterest

in one NSString

NSString *alertstr=[NSString stringWithFormat:@"No instances of %@",self.thisSpec.activityOfInterest];

after that call your UIAlertView and then rearrange the word delegate:nil into delegate:self

if (aOiCount == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertStr message:@"Please select an activity or Cancel" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}

Otros consejos

I don't think this is valid Objective C syntax:

initWithTitle:@"No instances of %@",self.thisSpec.activityOfInterest

You need to wrap it in an NSString, or a self-contained form.

Consider one of the many NSString methods, such as stringWithFormat or construct one in a different way. Either way, you should pass a complete string here.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top