How to hide the subView based on textfield value in iOS? - ios

I am having four SubViews and two TextFields stateText and providertext.
If I click the stateText it shows a UIPickerView with two values. If I select the first value in the UIPickerView the view should not change and if I select the second value, this means that only third SubView has to change. In the same time, the selected picker value populates to TextFields.
If I select second value means the third SubView changes. But TextField values are not populated. Only this newSubView becomes active. Here is my Code:
PickerView Done Action
if([stateText.text isEqual:#"SecondValue"])
{
[self newSubView];
}
-(void)newSubView
{
UIView *loginView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+160, self.view.frame.size.width, 150)];
loginView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:loginView];
}

Related

I am getting error like "Assignment to read only property" in UIView

If VIN is an text field this code works for me to Hiding keyboard without breaking UITextField functionality..
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
But in another case i need to do same thing for UIVIEW.. it is possible to do for UIView ?
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
In this case VIN Is an UIView.. i am getting error like "Assignment to read only property".
UITextfield has inputview. Default inputview is keyboard. uiview has not input view that you can set so you can't assign input view to UIView.
And for hiding keyboard you should call resignFirstresponder like,
[myTextField resignFirstResponder];
This will hide keyboard and when you click textfield it will show keyboard again so you should do like this.
Hope this will help :)
If you want to hide or disable keyboard for particular textfield , you can use following textfield delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here You can do additional code or task instead of writing with keyboard
if (textField == YourTextField)
{
return NO;//Keyboar Wont appear for textfield
}
return YES;
}

How to implement UIPickerView for the textfield in for loop?

I am displaying text fields in a UIView dynamically, based on JSON response data using a for loop.
I need to show a UIPickerView for the each text field in the for loop, but it is only working for the last text field. I am unable to display a UIPickerView for each of the text fields. Can anyone please help me to solve this issue?
So you are inside your for loop with an i index. What we are looking here is to assign a UIPickerView as each textfield's input view. Try something like this...
//FOR LOOP BEGINS
//Iteration - Create a textfield.
[self.view addSubview:yourTextfield];
//Assign a uipickerview as textfield's input view.
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.frame = CGRectMake(0,0,300,300);
pickerView.tag = i;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
textField.inputView = pickerView;
[self.view addSubview:pickerView];
//Iteration ends
//FOR LOOP ENDS
P.S. You can also add a toolbar with each pickerView to your textfield using the inputAccessoryView
You can set the number of rows to a large number, and make it start at a high value, there's little chance that the user will ever scroll the wheel for a very long time -- And even then, the worse that will happen is that they'll hit the bottom.
you can see this:
How do you make an UIPickerView component wrap around?

How to programmatically deselect UITextField

I have a UITextField which I can click on, and edit the contents of. I am running code when the UITextField is pressed by using:
[personalCountryLabel addTarget:self action:#selector(countryPressed) forControlEvents:UIControlEventEditingDidBegin];
This presents another view controller. However, when I click the back button, the UITextField is still selected, so the text runs again, sending me back to the view controller.
I use the code:
textField.enabled = false;
and
textField.enabled = true;
to respectively turn off and on the editing of the UITextField, but doing this in succession does not unselect the UITextField.
How can I therefore programmatically deselect the UITextField (i.e, where the line cursor is no longer blinking).
If I understand what you're asking correctly, you just want:
[textField resignFirstResponder];
/* Programmatically deselect the Uitextfiels below this Code */
UITextField *txtDeselect = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+20, self.view.frame.origin.y+20, self.view.frame.size.width-40, 40)];
[txtDeselect setBackgroundColor:[UIColor yellowColor]];
[txtDeselect setBorderStyle:UITextBorderStyleRoundedRect];
[txtDeselect setTextAlignment:NSTextAlignmentCenter];
[txtDeselect setText:#"Email"];
txtDeselect.enabled = NO;
[self.view addSubview:txtDeselect];
did you set outlet & delegate of UITextField in your view controller ?
why you use UItextfield for this ?
i recommend you use one of this before presenting new view controller :
option 1 :
[youtextfield resignFirstResponder];
//please sure you outlet connected & ....
you can call this on your viewWillAppear
option 2 :
[self.view endEditing:YES]; // this regularly
happend after present another VC)
you shouldn't use shouldEndEditing

How to select programmatically created text fields?

I am adding text fields to a view programmatically like this:
// Add a text field.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
textField.returnKeyType = UIReturnKeyNext;
[textField becomeFirstResponder];
[textField addTarget:self action:#selector(nextButtonPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];
UITextField *textFieldTwo = [[UITextField alloc] initWithFrame:CGRectMake(20, 160, 280, 40)];
textFieldTwo.returnKeyType = UIReturnKeyDone;
[textFieldTwo addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];
How can I select these fields later on?
I know how to do this when I'm creating things using UI, but how does this work for dynamically added elements?
Example: I want to focus the second field when "Next" button is pressed.
Thanks!
There are at least two ways of doing it:
Give each of your dynamically created fields a distinct tag, and then retrieve the required field using the tag that you gave it by calling viewWithTag: on the view to which you added your fields, or
Make textField and textFieldTwo instance variables of your class, initialize them when you have to, and then refer to these ivars later when you want to send input to them.
The second way is close to what you do when you add the fields through the Interface Builder. The only difference is that in this case the fields are added programmatically.
In this case, I usually set a tag for each textfield present on my viewController, and assign them my viewController as delegate :
// You should use const to identify quickly your tag
textField.tag = 10;
textFieldTwo.tag = 11;
textField.delegate = self;
textFieldTwo.delegate = self;
Then I implements the textFieldShouldReturndelegate method :
#pragma mark - UITextFieldDelegate protocol conformance
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
By doing so, you can have multiple UITextField with a focus moved from one to another without having to implements multiple UIControlEventEditingDidEndOnExit event methods.
You can get these by there tag value. For this
1.. set a unique tag value, to each textField at time of creation. (like 45 for first, and 78 for second textfield)
textField.tag = 45;
2.. suppose your you have added these textField as subView on 'myView'.
UITextField *txtField = (UITextField*)[myView viewWithTag:45];
this line will give you textfield having tag 45, which is added on myView.
Note -- Avoid to use '0' as tagValue for any control because '0' is used as byDefault tagValue for controls.
On UIControlEventEditingDidEndOnExit event for the first textField, call [textFieldTwo becomeFirstResponder].

uipickerView is in the middle of view and change default inputview position

I have create an iPad application of data entry.
I use a Tableview for data entry end the cells is custom cell.
For the date I have created a textfield with a pickerview (UIDatePicker)
pickerView = [[UIDatePicker alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
pickerView.datePickerMode = UIDatePickerModeDate;
self.dateField.inputView = pickerView;
The problem is that when I press on the textfield, it shows the pickerview in the middel of view and when I press the "Done" button to hide the pickerview the defaul position of the inputview (default keyboard) is not in the bottom of view but in the middle. If press again on the "Done" button of the picker view, the keyboard position increase its x value.
The problem is because when I show the pickerview, the keyboard is undocked (iOS5).
Is it possible to set the undocked - docked state of the keyboard in the code?
I have insert image of my problem:
Foto default keyboard
Foto Pickerview

Resources