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.
Related
I am trying to put notifications in an array as they become available, but the count of the array is reset to 1 when I push a new notification.
This is the code:
int r = 0;
listMsgReceived = [[NSMutableArray alloc] init];
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Notification received" message:[NSString stringWithFormat:#"%#", message] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
[listMsgReceived insertObject:message atIndex:r];
r++;
NSLog(#"apres: %d \n", [listMsgReceived count]);
}
It looks like you are initializing the variables r and listMsgReceived each time your notification is received (though it's hard to tell from the context you provided).
You should not do that, because that gets you a new array each time, where you insert one object - hence the count will be one after each notification.
You could try moving your array initialization outside of your method; declare it as a property on your class and initialize it in the initializer.
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.
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];
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
}
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];