how to set validation on button current title - iphone

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?

Related

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...

UIActionSheet in Landscape has incorrect buttonIndicies

I have an action sheet that is causing me grief on the iphone in Landscape orientation. Everything displays just fine, but in Landscape, the first real button has the same index as the cancel button and so the logic doesn't work.
I've tried creating the actionSheet using initWithTitle: delegate: cancelButtonTitle: destructiveButtonTitle: otherButtonTitles: but that was just the same, my current code is as follows;
UIActionSheet* actionMenu = [[UIActionSheet alloc] init];
actionMenu.delegate = self;
actionMenu.title = folderentry.Name;
actionMenu.cancelButtonIndex = 0;
[actionMenu addButtonWithTitle:NSLocalizedString(#"str.menu.cancel",nil)];
[self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu showInView:[self.navigationController view]];
[actionMenu release];
The addActiveButtons method basically configures which buttons to add which it does using code like this;
[menu addButtonWithTitle:NSLocalizedString(#"str.menu.sendbyemail",nil)];
There are perhaps 6 buttons at times so in landscape mode the actionSheet gets displayed like this;
My delegate responds like this;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"Cancel Button Index is : %d",actionSheet.cancelButtonIndex);
NSLog(#"Button clicked was for index : %d",buttonIndex);
NSString *command = [actionSheet buttonTitleAtIndex:buttonIndex];
DLog(#"COMMAND IS: %# for index: %d",command,buttonIndex);
if ([command isEqualToString:NSLocalizedString(#"str.menu.sendbyemail",nil)]) {
// Do stuff here
}
if ( ... similar blocks ... ) { }
}
In the example shown, I am finding that cancelButtonIndex is 0 as expected, but so is the button index for the first other button! This means if I click on the second (Save to Photos) button for example, my debug output looks like this;
Cancel Button Index is : 0
Button clicked was for index : 1
COMMAND IS: Send by Email for index: 1
I've tried various permutations and am now tearing my hair out wondering what I'm missing. I've had a good search around but the other problems people seem to be having are display issues, rather than functionality ones.
Can anyone see where I've gone wrong?
PS. I know this isn't the greatest UI experience, but I figure that most users will actually be in portrait most of the time or using the iPad version of the app so I'm prepared to accept the actionsheet default behaviour for landscape assuming I can get it to actually work!
OK, fixed it by counting how many buttons I was adding and then adding the cancel button as the last option, so my code looks like this;
int added = [self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu addButtonWithTitle:NSLocalizedString(#"str.menu.cancel",nil)];
actionMenu.cancelButtonIndex = added;
Hope that helps someone else struggling witht the same issue!
I ran into the same issue even though I already was including the Cancel Button as the last one in the action sheet and setting its index accordingly. My problems had to do with the 'Destructive' button. After some investigation, here is my take on the problem:
After N buttons have been added to the actionsheet, it switches it's layout to put the Destructive button at the top and the Cancel button at the button. In between is a scrollable view that includes all of the other buttons. Other sources indicate that this is a a table view.
For the iPhone, N is 7 for Portrait orientation and 5 for Landscape orientation. Those numbers are for all buttons including Cancel and Destructive.
It does not matter where in the action sheet you had originally put the Cancel and Destructive buttons within the action sheet. Once the limit has been reached, the Destructive button is moved to the top and the Cancel is moved to the bottom.
The problem is that the indices are not adjusted accordingly. So, if you did not initially add the Cancel as the last button and the Destructive as the first, the wrong index will be reported in actionSheet:clickedButtonAtIndex: as the initial report stated.
So, if you are going to have more than N buttons in your action sheet you MUST add the Destructive button to the actionSheet as the first button to the action sheet. You MUST add the Cancel button as the last button added to the action sheet. When initially constructing the sheet just leave both as nil, as described in another answer.
I had the same problem. To fix it, I just create an actionSheet with nil for all the buttons, and added buttons manually afterwards. Lastly, in the handler, ignore the firstOtherButtonIndex because it will be wrong (even if you set it ahead of time). Instead, assume that it is 1 because index 0 is the cancel button in this example. Here's the code:
NSArray *items = [NSArray arrayWithObjects:#"one", #"two", #"three", nil];
UIActionSheet* actionSheet = [[[UIActionSheet alloc] initWithTitle:#"Title" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
[actionSheet addButtonWithTitle:#"Cancel"];
for (NSString *title in items) {
[actionSheet addButtonWithTitle:title];
}
[actionSheet addButtonWithTitle:#"Destroy"];
// set these if you like, but don't bother setting firstOtherButtonIndex.
actionSheet.cancelButtonIndex = 0;
actionSheet.destructiveButtonIndex = [items count]+1;
Also, don't forget to show this from a tab view if you're on an iPhone because the tab bar steals touch events and prevents the lower button from being hit.
My solution is to initialize like this specifying only the destructiveButtonTitle...
UIActionSheet * as =[[[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:#"Cancel"
otherButtonTitles:nil] autorelease];
[as addButtonWithTitle:#"Button 1"];
[as addButtonWithTitle:#"Button 2"];
That way you get the Cancel button at index 0 always and your own buttons begin at index 1 even when there is a scroll view.

disable Alert view button

I have an alert view for twitter posting.
Alert view has 2 button and a textfield
send and cancel
I want to disable send button, until user fills the message box(i.e textfield).
like,empty field kind of validation.
How can I disable send button?
I had a similar requirement and was able to do this without resorting to anything explicitly prohibited by Apple (ie, the use of private classes or API's). In the example below, I find and then disable the "Recover" button.
Note #1 -- The placement of "[alert Show]" is important. It (apparently) lays out the views, so must be done before attempting to look through the view hierarchy.
Note #2 -- the "contains:" method is one I defined that does an NSString case-insensitive substring search. Use rangeOfString perhaps in your code.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Application Warning"
message:#"What should I do with the file?"
delegate:self
cancelButtonTitle:#"Ignore"
otherButtonTitles:#"Remove", #"Recover", nil];
[alert show];
// try to find and disable "Recover" button
for(UIView *aView in alert.subviews)
{
if ([[[aView class] description] contains:#"Button"])
{
UIButton *aButton = (UIButton *)aView;
if ([aButton.titleLabel.text contains:#"Recover"])
{
aButton.enabled = NO;
}
}
}
This is not possible with the current SDK. You will have to create a custom view to take the user's input. The fact you are adding a textfield to the UIAlertView is itself unsupported and could break in any future SDK anyway.
I would suggest you create a custom view and if you still want it to look like a UIAlertView you can do this with appropriate images and custom buttons.

making a text field required in objective-c

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