Showing an alert with Cocoa - ios

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

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

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
}

Password confirmation with 2 UITextFields

I want the user to confirm the password he typed it... so I use two text fields.. But somehow even if both have the same password it seems to think that the 2 strin differ
if (![self.typePTextField.text isEqualToString:self.retypePLabel.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.retypePLabel.text = #"";
return;
}
The alert appears even if I type the same string twice... and only the first text field geets reset to #"" ...
What will fix this?
I'm guessing in the isEqual: method
self.retypePLabel.text
should be
self.retypePTextField.text
try
if (![self.typePTextField.text isEqualToString:self.sometextfield.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.sometextfield.text = #"";
return;
}
if([_txtPassword.text isEqual:_txtconfirmPassword.text])
{
NSLog(#"Password =%# , ConfirmPassword = %# ",_txtPassword.text,_txtconfirmPassword.text);
}
else {
//// code show alert////
}
self.retypePLabel.text? are you sure you ask about this, because I think it should be like self.retypePTextField.text

Expression Result Unused in iOS UIAlertView

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

Resources