UITextView not picking up text input iOS - ios

i have UITextView and i want to show a alert if empty
.h
#property (nonatomic, strong) IBOutlet UITextView *textField;
.m
- (IBAction)next:(id)sender {
if ([self.textField.text length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error!"
message:#"text empty"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
alert is not showing and i can move on without any text being inputted. can anyone help me figure out why?
thanks

I checked this from my end and its working perfectly.
Here is download link for testing the project
I am sure, textview is not connected to IB.
If its is connected to IB, then I would say to upload sample project on dropbox to check from our end.

There is a possibility of the textField having whitespace characters. Try trimming out any possible whitespace using the following:
NSString *text = [self.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet];
if ([text length] == 0) {
....
}

Try this -
- (IBAction)next:(id)sender {
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *trimmed = [self.textField.text stringByTrimmingCharactersInSet:whitespace];
if ([trimmed length] == 0) {
// Text was empty or only whitespace.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error!"
message:#"text empty"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}

Related

Show message depending on typed text in textfield

Good day! i have this code for UIAlertView that when you press the button it will show a message what ever you typed.
- (IBAction)sellClick:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Confirmation"
message: #"Message"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
[alert release];
}
now what i want is what if i have 3 or more UITextfields and when i press the button i want to show all the typed text in UItextfield to UIAlertView for example in my 1st text field i typed. "WANT" and 2nd is "LARRY" and 3rd is "PLAY" when i press the button it shows the alert message, "I WANT LARRY to PLAY" , how can i make it like that? thanks!
You can create string with your uitextfields like:
- (IBAction)sellClick:(id)sender {
NSString *message = [NSString stringWithFormat:#"I %# %# to %#", textField1.text, textField1.text, textField1.text]
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Confirmation"
message: message
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
[alert release];
}
Hope this help.
If your text fields are added to the view in XIB then make sure you have your IBOutlets connected to your textfields. Such as:
#property (weak) IBOutlet UITextField* textField1;
#property (weak) IBOutlet UITextField* textField2;
#property (weak) IBOutlet UITextField* textField3;
Then you could simply compose a message:
NSString *message = [NSString stringWithFormat:#"I %# %# to %#",self.textField1.text,self.textField2.text,self.textField3.text];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Confirmation"
message: message
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
...

Multiple UIAlertViews to Enter Data

I have two buttons that will pop up an alertview with textfield to input data. However, only certain characters are allowed in each of the two textfields. Somehow, if I press the second button, the character set from the first button is used. What's going on here?
Also, what would be a more elegant form of inputting data without having to use an alertview? Could I use a modal view? If so, how?
- (IBAction)editRate
{
if(!self.powerOn) return;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit Jail Fee Rate"
message:#"Enter New Daily Rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = #"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers are allowed in this field."delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil];
[errorAlert show];
FeeRate.text=#"0.00";
}
else
{
FeeRate.text = field.text;
[[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else
{
}
}
- (IBAction)editDate
{
if(!self.powerOn) return;
UIAlertView *alertDate = [[UIAlertView alloc] initWithTitle:#"Edit Jail Fee Date"
message:#"Enter New Date"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertDate.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alertDate textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alertDate show];
}
- (void)alertDate:(UIAlertView *)alertDate clickedButtonAtIndex:(NSInteger)buttonIndex2
{
if (buttonIndex2 != alertDate.cancelButtonIndex)
{
UITextField *fieldDate = [alertDate textFieldAtIndex:0];
fieldDate.placeholder = #"Enter New Date";
NSCharacterSet * setnew = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789/"] invertedSet];
if ([fieldDate.text rangeOfCharacterFromSet:setnew].location != NSNotFound)
{
UIAlertView *errorAlert1 = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers and slashes are allowed in this field."delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil];
[errorAlert1 show];
FeeDate.text=#"00/00/0000";
}
else
{
FeeDate.text = fieldDate.text;
[[NSUserDefaults standardUserDefaults] setObject:fieldDate.text forKey:DATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else
{
}
}
When a UIAlertView is dismissed then a function called
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
is called.
So, both your alert views call this function.
One way to tell which alertView is calling it, you could create an ivar or better two properties like this:
#property (nonatomic, strong) UIAlertView *rateAlert;
#property (nonatomic, strong) UIAlertView *dateAlert;
and you should initialize like this:
[self setRateAlert:[[UIAlertView alloc] initWithTitle...
[self.rateAlert show];
and
[self setDateAlert:[[UIAlertView alloc] initWithTitle...
[self.dateAlert show];
and then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView==self.rateAlert) {
//do whatever for rateAlert
} else {
//do whatever with dateAlert
}
}

Check if UITextfield contains (? ! $ , etc)

Im doing a conditional statement that checks if a textfield contains certain characters, if it does they an UIAlertView will show. At the moment if the text field contains letters then an the alert is shown, how would I extend my method below to include characters such as ? ! , $ etc.
if ([self.withdrawTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]].location != NSNotFound) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
Try this:
NSMutableCharacterSet *mcs1 = [[NSCharacterSet letterCharacterSet] mutableCopy];
[mcs1 addCharactersInString:#"?!.$"];
if ([self.withdrawTextField.text rangeOfCharacterFromSet:mcs1].location != NSNotFound) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
As you want only numbers and dot in the string than you can try below code. It may help you.
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789."];
for (int i = 0; i < [[self.withdrawTextField.text length]; i++)
{
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c])
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}

UIAlertView handle textfield

i want to do a simple alertView and ask for the phone number...
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"What is your phone number?"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
message.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* answerField = [message textFieldAtIndex:0];
answerField.keyboardType = UIKeyboardTypeNumberPad;
answerField.placeholder = #"+43 ...";
[message show];
And what do i have to put here into to check up something with the number (Like Mysql, etc.)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];
?????????
// Something like if (textField == "090012345678") -> NSLog(#"WOW"); ...
}
If you don't know how to compare if two strings are the same then may I recommend that you seek out some basic tutorials on Objective-C.
if ([textField.text isEqualToString:#"090012345678"]) {
NSLog(#"WOW");
}

How to create two textfields in an alert view, IOS

I want to create an alertview with two uitextfields inside of it.
method:
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Enter File Details"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add", nil];
UITextField *textFieldDescription = [message textFieldAtIndex:0];
textFieldDescription.placeholder = #"File Description : Ex. Acat Briefing";
UITextField *textFieldFileName = [message textFieldAtIndex:1];
textFieldFileName.placeholder = #"Exact File Name : Ex. acat.pdf";
[message show];
}
//make sure file description is long enoguh
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] <= 15 && [inputText length] >= 4)
{
return YES;
}
else
{
return NO;
}
}
//handle add button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Add"])
{
UITextField *fileDescription = [alertView textFieldAtIndex:0];
UITextField *fileName = [alertView textFieldAtIndex:1];
NSLog(#"Desc: %#\nName: %#", fileDescription.text, fileName.text);
}
}
Error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'
Why I do get this error, how can I create two uitextfields in an alert view?
=========Working Solution ===========
Thanks for the answer below works when you only need two plain textfields
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Enter File Details"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add", nil];
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
UITextField *fileDescription = [message textFieldAtIndex:0];
fileDescription.placeholder=#"Ex. acat.pdf";
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
UITextField *fileName= [message textFieldAtIndex:1];
fileName.placeholder=#"Ex. Acat Briefing";
[message show];
}
After you allocated the "message" alert view. Add this to your code:
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
This will make your alert view two text field inside.
The error you received occurs because there are no text fields in your UIAlertView, 'message'. The instance method "textFieldAtIndex" exists to access textFields in a UIAlertView that is created with a specific style, like UIAlertViewStylePlainTextInput, UIAlertViewStyleSecureTextInput, or UIAlertViewStyleLoginAndPasswordInput. These styles are set on the property "alertViewStyle". For example:
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
You may use "textFieldAtIndex" after setting this property, but unfortunately it looks as if none of these styles suit your needs.
What I've done before is to create a default styled UIAlertView (like you've already done), and add UITextFields as subviews to the UIAlertView.
For Example:
//Create the alert then add any labels and text fields as subviews.
//You can pad out an alertView by adding newline characters in the message. This will
// give the alertView more space to draw the text fields.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Two Text Field Alert"
message:#"\n\n\n\n\n"
delegate:self
cancelButtonTitle:#"CanceL"
otherButtonTitles:#"OK", nil];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[message addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(16,112,252,25)];
textField2.placeholder = #"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[message addSubview:textField2];
[message show];
[message release];
[textField2 release];
[textField1 release];
It's a lot more verbose and messy to do a login this way as opposed to the alertView styles, but you can adapt this as you see fit to add any number of subviews to an alert view.
Edited to simplify example.
You're getting that error because the UIAlertView does not contain any textfields. Since the alert view's textfield collection is empty when you try to call [alertView textFieldAtIndex:0], you end up with the NSInvalidArgumentException and a crash.
We are allowed to create only two text fields in alertview. Here:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Change Password"
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField * alertTextField1 = [alert textFieldAtIndex:0];
alertTextField1.keyboardType = UIKeyboardTypeDefault;
alertTextField1.placeholder = #"Type Current password";
[[alert textFieldAtIndex:0] setSecureTextEntry:YES];
UITextField * alertTextField2 = [alert textFieldAtIndex:1];
alertTextField2.keyboardType = UIKeyboardTypeDefault;
alertTextField2.placeholder = #"Type New Password";
[alert show];
Here is the solution for your question..
http://www.alterplay.com/ios-dev-tips/2009/12/username-and-password-uitextfields-in-uialertview-prompt.html

Resources