making a text field required in objective-c - iphone

I'm currently in the process of developing an app for the iPhone. There is a screen which requires users to enter their data in text format but it can easily be skipped by simply clicking the 'submit' button. Is there a way I can make these text fields required?

Have you tried de-activating the button unless the text field has something in it?

u can tell the button process to keep an eye on the text field so that if the text field is empty, the button wont process the function it would be like this
-(IBAction) buttonPressed:(id) sender
{
//----this will process the button function if the textfield is not empty
if(textField.text != #"")
{
>>> do the process <<<
}
else
//----this will show an alert message when the user tries to click button without filling the textfield
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error"
message:#"please fill the information first"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}

Related

UIAlertView maximum text length

I just want to show some text on the UIAlertView but it is showing "Null" string if the text length larger than allowed size.
I am pretty sure the text will not larger than the screen (about a bit more than half), so I don't want to make it be complicated to implement a ScrollView for that.
I follow problem in changing size of uialertview to change the size of AlertView, but it does not work, additionally produce some weird visual effect.
I tried this third part component https://github.com/inamiy/YIPopupTextView, I can't even pass the compilation. (already import that 4 files into project.) I don't know why.
So, actually I just want to increase the size of text that allowed to show on AlertView. Any idea?
I wrote a tiny test program. Here's the only thing I added to the Single View Application template:
- (void)viewDidAppear:(BOOL)animated {
NSMutableString *message = [[NSMutableString alloc] init];
while (message.length < 100000) {
[message appendString:#"Hello, world! "];
}
[message appendString:#"This is the end."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test" message:message delegate:nil cancelButtonTitle:#"Done" otherButtonTitles:nil];
[alert show];
}
This works fine on the iPhone simulator running iOS 5.0, and on my iPhone 5 running iOS 6.0.2. All the text is displayed (in a scrollable text view).
Your problem is probably not with the size of the text.
I found a strange behavior while I was showing the result of a JSON object parsed in a dictionary and then printed on on an alertView (on Xcode 5.1.1, compiling for iOS 7.1 on iphone 64 bit simulator). For the same inputData:
[[UIAlertView alloc]initWithTitle:#"something" message:[[[NSString stringWithFormat:#"json:%#",[inputData dictionaryRepresentation]] stringByReplacingOccurrencesOfString:#" "withString:#""]substringToIndex:7035] delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil];
correctly prints, but if I say "substringToIndex:7036" it shows only blank space...without "stringByReplacingOccurrencesOfString:" method the limit is far beyond:
[[UIAlertView alloc]initWithTitle:#"something" message:[[NSString stringWithFormat:#"json:%#",[inputData dictionaryRepresentation]] substringToIndex:13768] delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil];
correctly prints, instead "substringToIndex:13769" not prints...I realize that is not a matter of maximum length but of special characters inside the JSON object

AlertView with textfield - beginner

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"HELOOOOOOOO" message:#"write a number of text-lines/paragraph here"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 95, 260, 25)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
If you look close you will find that the message attribute is quite long. I need this alertview to first display the title, and then my long message, and then the text field and finally the 2 buttons.
But what hapence here, is that since the message is too long, the textfield overlaps with the buttons.
How can i solve this ?
If this is for iPhone, you really can't. The UIAlertView is not meant to handle input. Extended messages are shown with a scroll view but you really should not add a UITextField to it's view hierarchy (for one, it goes against their design standards and your app MAY get nixed!).
In this situation, it would probably be best to use a new UIViewController to handle showing your content.
Again, the only "actions" that UIAlertView is meant to provide is that of the multiple buttons.
Implement this piece of code in your implementation file.
- (void)willPresentAlertView:(UIAlertView *)alertView
{
alertview.frame = CGRectMake(12, 95, 260, 25); //You can set a frame that suits to your needs
}
This delegate - (void)willPresentAlertView:(UIAlertView *)alertView will invoke before showing the alerview, so it is a better way to set frame to your alertview.
For more informations on aleert views:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intf/UIAlertViewDelegate
Actually, it doesn't matter how long your message is. The alert view will always size so it fits.
One way you can fix this is by adding a bunch of \n at the end of your message string, which is equivalent to putting a line break.
EDIT: If your message is so long that it puts it in a UIScrollView, there's nothing you can really do unless you're fine with major hacking (aka, changing the bounds of the UIAlertView, moving each button down, etc.).
A second way works only on iOS 5 and newer. You can set the UIAlertView's alertStyle property to UIAlertViewStylePlainTextInput which will display an alert view with a text field.
Use newline or line break character \n for this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"AlertView"message:#"\nhello:.................... \nwrite here.................. \nwrite here....................." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
Check this blog post for more help

UIAlertView with UITextField and long message

I am using this code to implement a UITextField in my UIAlertView:
UIAlertView *receivedAlert = [[UIAlertView alloc] initWithTitle:message message:[NSString stringWithFormat:#"%#\n\n", [itemNameRows objectAtIndex:currentItem]] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
receivedField = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,25)];
receivedField.keyboardType = UIKeyboardTypeNumberPad;
receivedField.textAlignment = UITextAlignmentCenter;
receivedField.borderStyle = UITextBorderStyleRoundedRect;
receivedField.keyboardAppearance = UIKeyboardAppearanceAlert;
[receivedAlert addSubview:receivedField];
[receivedAlert show];
[receivedAlert release];
The problem arises when I have a message that is longer than 2 lines. The alert view resizes properly but the text field doesn't move down to accommodate the longer line. Any ideas?
I can settle with shortening the message string if I have to.
You shouldn't add subviews to UIAlertView. Apple's docs explicitly prohibit it ("The view hierarchy for this class is private and must not be modified."), and in addition to possibly getting you rejected, could also break in a future OS update (this happened once already around the iOS 3.1).
iOS 5 added alert view styles so that you can use text fields in alert views in a safe way. In addition to UIAlertViewStyleDefault, there are UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, and UIAlertViewStyleLoginAndPasswordInput.
Create an alert view and set the alertViewStyle property to the appropriate value. You can then get the text field(s) by using -textFieldAtIndex:. Secure and plain text styles have a single text field at index 0, default has no text fields, and login and password have two text fields at indexes 0 (login) and 1 (password).
See the UIAlertView Class Reference for more information.

How to do validation on textfield?

I am making a registration form, in which I have 8 text fields and one submit button.
Whenever the user fails to enter one of the text fields, upon click of submit button an error message should be generated.
And when the user fills all the text fields, on click of submit button it should go to the next page.
Please give me some advice, thanks.
Simple logic, in your Submit action check that your textField.text is not nil or empty (#""). If not textField.text show UIAlert. Like this
-(IBAction) submitButton
{
if(self.txtName == nil || [self.txtName.text isEqualToString:#""])
{
[self showErrorAlert];
}
if(self.txtEmail == nil || [self.txtEmail.text isEqualToString:#""])
{
[self showErrorAlert];
}
}
// and show error alert as
-(void) showErrorAlert
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#""
message:#"All Fields are mandatory." delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}
-(void)emptyTextfieldVaildation
{
if( ([TxtFieldName.text isEqualToString:#""]) || ([TxtFieldPaswrd.text isEqualToString:#""]) )
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error!!"
message:#"Please fill in the details." delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}
else
{
// Action to be called on Submit button touch
}
}
Set the method on click of submit button
-(IBAction)submit:(id)sender
{
//Do all the textField Validation
}
Check the all textfield data by using
[textfield.text isEqualToString:#""]
put this into if statement for all text field.. If condition is true , then show alert View
You have to check all fields before, and there are several way to do this. You can control the lenght of text of each field and show an alert if some field is empty. You can check the length property of the NSString, so you can also operate on the length(e.g the password must be 8 char, otherwise Alert.).
If you have tagged these 8 textFields(or if you know that there are just this textfields in your view) a good way may be like this:
for(UITextField * tf in [self.view subviews]){
if(![tf.text length]>0){
//show the alert
}
}
hope this helps.
Last night I struggled with this too and I used
if ([myTextField.text length] == 0) {
// error code to handle empty text field.
}
to handle empty text field. It works, for empty text field, of course, but failed if there are some spaces in there. Some people in SO give suggestion to trim the string first before evaluate using the code above. See link
How to enable a UIButton if a textfield is not empty?
set tag property of all your text fields uniquely and access them using if(textFieldName.tag==*yourtag*) in button action event...

how to set validation on button current title

hi i am new in iphone development,and i have task to develop the uibutton and picker relational application.
i have to develop the one view which have the button 1 uipicker view. when i run the application at that time the title of the button is blank. if the user directly click to submit the button at that time user will get the error message.
but i can get the error msg and my following code is not working it is submiting the form without checking the button text validation.
following is my code:
if ([btnPicker.currentTitle isEqualToString:#" "]) {
UIAlertView *alertPicker = [[UIAlertView alloc] initWithTitle:#"Required" message:#"Please,Select Month & Date." delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No",nil];
[alertPicker show];
NSLog(#"%#", alertPicker);
[alertPicker release];
}
[btnPicker.currentTitle isEqualToString:#""];
or
if ([btnPicker.currentTitle length]==0)
Actually a blank string is an empty string #"" not a space #" ". You have to test an empty string like this,
[btnPicker.currentTitle isEqualToString:#""];
Shouldn't it be [btnPicker.currentTitle isEqualToString:#""] ? String without the space?