UITextField Validation not working - iphone

I am new to iPad developer,
I am making Registration form in my iPad, when user click Submit button i want to validate my textfield whether it is empty or not, if it is empty then it will display alert No cannot be empty.
here is my code snippet but it is not working,
declaration:
UITextField* noTxtbox= [[UITextField alloc] initWithFrame:CGRectMake(250, 200, 180, 50)];
noTxtbox.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:noTxtbox];
Code:
if ([noTxtbox.text isEqualToString:#""]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Enter your No !"
message:nil delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
Any help will be appreciated.

try using noTxtbox.text.length == 0

The problem could be due to white space. Check it by removing white space by :-
NSString * noTxtbox_text = [noTxtbox.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([noTxtbox_text isEqualToString:#""]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Enter your No !"
message:nil delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}

Related

Uistepper in Uialertview

How can I show UIStepper in UIAlertView? Also suggest me any other alternative to show UIStepper in pop-up.
Try this.. Hope it will help you
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)];
UIAlertView *alert =[[UIAlertView alloc]init];// you can set your frame according to ypur requirment
[alert addSubview:stepper];
[alert show];
A better way of inserting views in UIAlertView would be to add them as a subview
Try the following
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"\n\n" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
UIStepper* stepper = [[UIStepper alloc] init];
stepper.frame = CGRectMake(12.0, 5.0, 100, 10);
[alert addSubview:stepper];
[alert show];

How to change the frame of AlertView in iPhone App...?

I want to Add TextBox on AlertView. But When i'm adding, it overlaps little with buttons on it...So i want to increase the height and width of alertview...Could Anybody tell any idea??
Try the following code:
UIAlertView alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Title", nil)
message:NSLocalizedString(#"Message\n\n\n", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil), nil];
UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(16.0, 100.0, 250.0, 25.0)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[alertView addSubview:textField];
[textField release];
[alertView show];
[alertView release];
try this
UITextField *TextField = [[UITextField alloc] initWithFrame:CGRectMake(22.0, 50.0, 240.0, 30.0)];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Enter\n\n" message:#"" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView addSubview:TextField];
[alertView show];

How to change UIAlertView cancel button text

I would like to know that how to change UIAlert's cancel button text during runtime.
- (void)showAlertWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelTitle {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:nil];
[alertView show]
[alertView release];
}

objective-c : iphone programming : Showing variables on UIAlert view

i want simply to show a double value on a uialert view it is possible?
Put the value into a formatted NSString and send that to the alertView:
NSString *messageString = [NSString stringWithFormat:"#number = %.2f", 42.0];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Title text" message:messageString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
How are you showing the UIAlertView now? Something like the following should work:
double myDouble = 12.6;
NSString *alertString = [NSString stringWithFormat:#"%g", myDouble];
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:alertString
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[myAlert show];

Showing a alertview with a textbox in iPhone

Is it possible to show an alertview with a textbox inside like the AppStore app.
It asks for password in such a dialog.
I've seen atleast a couple of other third party apps using it. Is it a private API?
Here's an "Apple Approved" way of doing it from Tharindu Madushana. I got it from his comment in this page: http://iosdevelopertips.com/undocumented/alert-with-textfields.html
// Ask for Username and password.
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Twitter Details!" message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
// Adds a username Field
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
utextfield.placeholder = #"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:utextfield];
// Adds a password Field
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
ptextfield.placeholder = #"Password";
[ptextfield setSecureTextEntry:YES];
[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield];
// Move a little to show up the keyboard
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0);
[alertview setTransform:transform];
// Show alert on screen.
[alertview show];
[alertview release];
//...
// Don't forget to release these after getting their values
[utextfield release];
[ptextfield release];
And finally to get the text back
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
return; //Cancel
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (#"%#", field.text);
}
Yes, it's undocumented. To add a text field to UIAlertView, use addTextFieldWithValue: label: method. You call with the default text as the first argument and the text that displays in an empty field as the second. Having done that, you can access the field via using textFieldAtIndex:n - see below.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Who are you?"
message:#"Give your full name"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#""label:#"Name"];
// Customise name field
UITextField* name = [alert textFieldAtIndex:0];
name.clearButtonMode = UITextFieldViewModeWhileEditing;
name.keyboardType = UIKeyboardTypeAlphabet;
name.keyboardAppearance = UIKeyboardAppearanceAlert;
[alert show];
The next snippet shows how to retrieve the value in the name field:
NSLog("Name is %#", [[modalView textFieldAtIndex:0] text]);
This is a really old questions with really old answers.
This is a sample of how I get a UITextfield into a UIAlertView since ios 5:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"New List Name" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
message.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
message.delegate = self;
[message show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this:
#property (nonatomic, strong) UITextField *alertTextField;
Jeff Lamarche posted some sample code on his blog to do just this. The formatting looked a bit wonky when I tried it but it's probably a good starting point.