How to implement UIPickerView for the textfield in for loop? - ios

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?

Related

Reuse the same pickerview for different textfields in tableview

I want to display the pickerview as input for textfield in the tableview. I am reusing the same cell xib for all the rows in tableview. at indexpath.row == 1 want to change the age, so the input for textfield is pickerview containing numbers from 18 to 100. In the next row when click on textfield i need to change gender, so the data in picker view should be male and female. Please help me in achieving this.
Thanks in advance.
You can simply reload the picker view:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[Yourpickerview reloadAllComponents];
}
Set the tag of the textfield and in textFieldShouldBeginEditing get the tag and then add
pickerVw = [[UIPickerView alloc] init];
pickerVw.dataSource = self;
pickerVw.delegate = self;
pickerVw.tag = TAG_PICKER;
// ... ...
[pickerVw reloadAllComponents];
Please See the GitHub link Has demo made for the purpose in swift
Thanks

UITextField, set placeholder after text in ios

I need to keep the placeholder after the text, when click on the text box i need to clear only the placeholder and able to type characters.
In this Marissa is the name text and (First Name) is placeholder. Once i start editing placeholder need to clear and start editing complete text.
How can i achieve that?
I have found some similar custom textfield in the below URL. If you find this suitable, you can use it.
TUTORIAL / SOURCE
Please refer below code.
Just copy-paste below code in viewdidload()
textfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"Yourtext%#",Placehodertext] attributes:#{NSForegroundColorAttributeName: color}];
Assign Delegate of UITextfield to self.
textfield.delegate = self
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
textfield.text = #"Yourtext"
return YES;
}
You can not set place holder along with the text in a textfield.
But there is one alternate for this.
Textfields has something called left view which you can make readOnly view.
Try this :
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
UILabel *textLabel = [[UILabel alloc] initWithFrame:paddingView.bounds];
textLabel.text = #"Marissa";
[paddingView addSubView:textLabel];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
Step 1:
Make the text filed attributed form xib and set the "Marisa (First Name)"
and change the color for the (First Name) as grey color .
Step 2:
And in the Textfiled DidBiginEditing method change the textField.text to "Marisa".
This could be achieved in several ways:
Use the attributedText property of UITextField instance to apply a styling for different parts of the text. Here you need to properly define the property each time a user has changed the input by using delegate methods or by observing UIControl Control Events.
If the structure of the text input is fixed and predefined you could use a batch of fields to imitate a more complex/smart field. Here, for example, you need to put two fields beside. One for the last name, second for the first name. Or you can use a combination of UILabel and UITextField if one the part is not editable. Such an approach allows you to configure a separate part of a complex field as you want: different placeholders, fonts, keyboard styles, etc.

UIToolbar not responding in Custom UITableViewCell with UIPickerView

I've searched this for a while but can't find anything quite the same.
I have a UITableView, and when a certain row is selected, I insert another row below.
The inserted row is a custom tableviewcell which hold a UIPickerView.
The pickerview works fine, and when an item is selected it can trigger the notification, sending selected info back to the tableviewcontroller, and then remove the "pickerviewcell". All good there.
But this isn't ideal if the user wants to scroll back and forth on the uipickerview. So I've added a uitoolbar to the uipickerview with a Cancel & Done button.
But the Cancel and Done buttons never get fired.
From other items I have read, they talk about UIFirstResponder etc etc, but they are all related to making the uipickerview an inputaccessoryview for a uitextfield. But that is not what I am doing.
I've tried doing it all in code and via Storyboards, with the same results each time.
Some example below..
// (in my CustomTableViewCell's AwakeFromNib function)
let screenSize: CGRect = UIScreen.mainScreen().bounds
pickerView = UIPickerView(frame: CGRectMake(0,0, screenSize.width, 162))
pickerView.delegate = self
pickerView.dataSource = self
pickerToolbar.barStyle = UIBarStyle.Default
pickerToolbar.translucent = true
pickerToolbar.tintColor = UIColor.orangeColor()
pickerToolbar.sizeToFit()
pickerToolbar.userInteractionEnabled = true
pickerView.addSubview(pickerToolbar)
self.contentView.insertSubview(pickerView, atIndex: 3)
// both these logs show correct output
NSLog("picker subviews: %#", pickerView.subviews.description)
NSLog("toolbar subviews: %#", pickerToolbar.subviews.description)
Screenshot example:
By clicking on the "To" cell, the new cell is inserted which has the picker. The picker works fine by itself. But the Cancel button doesn't get triggered. It has an IBAction linked to it from Storyboard.
Clicking any cell also closes/removes the pickercell correctly.
I have made a picker within a UITextview and I using
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;
and then dismissing the keyboard with
-(void)dismissKeyboard {
[_date resignFirstResponder];
}
where _date was my UITextfield property
Dismissing UIPickerView with Done button on UIToolBar
this is a really good post I found on this topic as well! I hope it helped.

How to disable keyboard when clicking a UITextField in iOS?

I am currently developing a prototype that I want to do user testing on desktop first before loading to iPad.
I am looking for solutions to disable the keyboard after clicking a textfield. That means after clicking a textfield, user is able to enter information from the macbook keyboard directly, and the virtual keyboard that automatically shows up in the simulator will not appear. I have been through a lot of tutorials but they are all dismissing the keyboard after user entry; that is not what I am looking for. How should I hide the keyboard?
Thanks so much!
Use this:
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor
It works for UITextField and UITextView and they need to be set to editable.
What you did Here:
You created a dummy view of width=hight=0, & assigned it as the inputView of your textField.
How It works:
Instead of showing default, keyboard, now, the viewController is showing DummyView as inputView for your UITextField. As DummyView has Width=height=0, You will not see anything on the screen :)
Here is another answer which I found the same hack but with little additional supportive code snippet to hide the blinking cursor too.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hides both keyboard and blinking cursor.
}
I needed this to be done for a Quantity text field where I increase/decrease the quantity using a UIStepper view. So I needed the keyboard to be hidden always.
This will set the inputView of your textField to, basically, an empty UIView with no frame.
self.theTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];

How to keep active UITextField when UIPopover is showing;

I have something like problem. When I activate UITextField to write something, I have addTarget to show popover below UITextField. When I keep typing popover shows suggestions.
There is bit of my code:
[googleView.search addTarget:self action:#selector(showPopover)
forControlEvents:UIControlEventEditingDidBegin];
-(void)showPopover{
suggestionTable *table = [[suggestionTable alloc]init];
self.suggestionpopover1 =[[PCPopoverController alloc] initWithContentViewController:table];
self.suggestionpopover1.popoverContentSize = CGSizeMake(300, 400);
self.suggestionpopover1.delegate = self;
[self.suggestionpopover1 presentPopoverFromRect:googleView.search.frame inView:googleView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
My problem is, that when popover is visible my UITextField and everything else isn't active.
How can keep active UITextField while I typing, and everything else should dismiss popover?
I found simillar thing in youtube app's search box.
You need to add the text field to the popover's passthrough views.
self.suggestionpopover1.passthroughViews = #[googleView.search];

Resources