Expression Result Unused in iOS UIAlertView - ios

I'm still a newb with iOS and I'm a little stuck creating an alert view combining regular text and variables. I'm getting an "expression result unused" warning on the initWithTitle line and I don't know how to fix it.
name = #"foo";
//now create the alert
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle: (#"Hello %#", name)
message: #"You're looking mighty fine today"
delegate: nil
cancelButtonTitle: #"I'm awesome"
otherButtonTitles: nil];
//show the alert
[myAlert show];
Right now, with the warning, everything compiles but my alert title is just "foo" instead of "hello foo".
If I remove the parentheses I get a syntax error on the next line.

Create the title as follows:
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle: [NSString stringWithFormat:#"Hello %#",name]
message: #"You're looking mighty fine today"
delegate: nil
cancelButtonTitle: #"I'm awesome"
otherButtonTitles: nil];

Related

Placeholders for variables in UIAlertView?

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

how do you localize the contents of an UIAlertview

Using Base Internationalization, It's become really easy to produce automatic localizations based on Object ID. Here's a french version of something.
/* Class = "IBUILabel"; text = "Clear Memory"; ObjectID = "fHm-5n-KrF"; */
"fHm-5n-KrF.text" = "Effacer la mémoire";
But that only extends to things that Xcode can find in the storyboard.
It should be possible to include text strings in an AlertView. Surely.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Wheel Cicumference" message:#"Ground Trace (mm):" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = 10;
[alert addButtonWithTitle:#"Accept"];
[alert show];
Apple docs are vast, and I haven't found anything meaningful so far. I don't necessarily wan't the answer laid out in full for me, Just set me off in the general direction
The only way i know to localize the UIAlert is the following:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error")
message:NSLocalizedString(message, message) delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK") otherButtonTitles:nil];

Making a delegate for a notification

Lets say i have an integer (hi) that is 0. I want a notification to say 0 in the message. my code is:
-(IBAction) alert3;
{
int hi = 0;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlertView"
message:#"%d"
delegate:hi
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
And then i get an error:
"Incompatible integer to pointer conversion sending 'int' to parameter of type 'id'"
Your implementation is incomplete. You need to add the message as a string.
Do this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlertView"
message:[NSString stringWithFormat:#"%d", hi]
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
%d is the parameter formatter for an int, that you have to pass after the comma
About the delegate, the delegate is the class that will respond to the messages defined in the UIAlertViewDelegate (for example, when the user touches a button).
If you dont want to control that, just set it to null:
delegate:nil
Or to self to control it yourself.

If Statement Advice Needed

I’m trying to show an alert when a value greater than 200 is entered in my UITextField, rcdAtIan.text. I’ve tried this:
Self.rcdAtIan.text = self.circuit.rcdAtIan;
if (_rcdAtIan.text > #"200");{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Value Exceeded" message:#"Message here............." delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
and then this:
Self.rcdAtIan.text = self.circuit.rcdAtIan;
if (self.circuit.rcdAtIan > #"200");{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Value Exceeded" message:#"Message here.........." delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
}
but the alert is being shown when the view is loaded, rather than by my if statement. I know what I’m trying to say — if (_rcdAtIan.text => 200 "show my alert"); — but I’m unsure of the correct syntax.
You're trying to perform an arithmetic comparison on 2 string values. What you need to do is ask:
if ([_rcdAtIan.text intValue] > 200) {...
You can't compare strings like this (well, you can, but it doesn't make sense, since it performs a numerical comparison of the pointers which represent the strings, and it does not compare the contents of the strings). So, you may want to convert the strings to numerical values:
if ([self.circuit.rcdAtIan intValue] >= 200) {
// "200" or more has been entered
}

Showing an alert with Cocoa

I have been using VB.NET for a while and I switched to Xcode 4.
In the VB.NET environment, I used to type the following command in a button:
if TextBox1.text="" then
MessageBox.Show("You can't leave the textbox empty!, "Error!")
else
Label1.text = TextBox1.text
That is just an example. In Xcode, I want to do the same thing except for I want to have a Pop-Up Alert (in the iPhone) instead of the MessageBox in VB.NET.
Any Ideas?
Here you go
if ([TextBox1.text isEqualToString:#""]){
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"You can't leave the textbox empty!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
} else {
Label1.text = TextBox1.text;
}

Resources