How to get data in UITableview instead of tapping on keyboard UITextfield. In UITableview, how to do multiple selection of data.
Edited:- I have a UITextfield. On tapping it, tableview should pop up with data in it instead of keyboard. When a row of tableview is selected, then checkmark should appear and that data should be seen in UITextfield.
Answer of your first question -
Your keyboard will hide by using this code.
-(BOOL)textField:(UITextField *)textField shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[self.view endEditing:YES];
// load your tableView here.
// Everything must be custom.that is your table view is just like a popup.
}
Answer of your second question -
Now current scenario is your table view is shown on screen.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedValue = [yourArray objectAtIndex:indexPath.row];
yourTextView.text = selectedValue;
yourTableView.hide = YES;
}
Here after selecting any row that value is shown in yourTextView.but after that you have to hide that tableView. Try this.May this will help you.
-(void)textViewDidBeginEditing:(UITextView *)textView
{
[self.view endEditing:YES]; // this will make key board hide
yourTableView.hide = NO; // here show tableview make sure tableview allocated out side and here you will be just showing.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourTextView.text = yourArray[indexPath.row];
yourTableView.hide = YES;
}
Use UITextField.inputView property.
More on the subject here:
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html
Related
In an iOS apps I have a table view that has 2 rows with textfield embedded inside.
Textfield2 is a textfield with pickerview input that I set with something like this:
textField2.inputView = pickerView;
So the thing is , the textField 2 icon on the right is only an image embedded in the textfield2.rightview that caused the image to not trigger the pickerView input when tapped.
Did select table view method also wasn't triggered because I set the textfield to fully occupy the cell. Thus, after some searching I find that disabling the textField2 user interaction enabling the didSelect to be triggered.
textField2.userInteractionEnabled = false;
However, now I'm at lost on to how to trigger the input to textField2 pickerView via the didselect tableview method. I tried this line of code but that doesn't work.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Did select row %d",indexPath.row);
if(indexPath.row == 1){
[textField2 becomeFirstResponder];
}
}
I tried to search how to trigger input manually to textField and didn't find any clue.
Thanks before ! :)
After some debugging with a clear mind, I find that
textField2.userInteractionEnabled = false;
caused the textfield not able be the responder.
So a little workaround with this flag is enable it first in order to edit the text field and disable it again after we finished editing. Something like this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Did select row %d",indexPath.row);
if(indexPath.row == 1){
[textField2.userInteractionEnabled = true;
[textField2 becomeFirstResponder];
}
}
- (void)donePicker {
// to enable the did select row again
[textField2.userInteractionEnabled = true;
}
I'm programming forms for my iOS app, and my UITextFields act strangely :
When the form is in update state (fields are already filled) I can edit and get the new input text (typed by user) with the .text attribute. But when the form is in insert mode the UITextFields are initially empty and I cannot get the text typed then by the user with the .text attribute because it always returns an empty string whatever the user types.
Here is a video showing the issue : https://youtu.be/tPJZ3sC-IFU
{
__weak IBOutlet UITextField *txtLibelle; //linked by ctrl + drag to the storyboard
NSString *libelleTemp;
}
- (void)viewDidLoad {
[super viewDidLoad];
// ...
[txtLibelle addTarget:self
action:#selector(editingChanged:)
forControlEvents:UIControlEventEditingChanged];
// ...
}
-(void) editingChanged:(id)sender {
libelleTemp = txtLibelle.text;
// libelleTemp is set to #"" whatever the user has typed in when I'm in insert mode
}
-(void)initWithLib:(NSString *)lib {
libelleTemp = [NSString stringWithString:lib];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if (indexPath.section == 0) {
txtLibelle.text = libelleTemp;
}
// ...
return cell;
}
The initWithLib: method is called from the previous view controller like this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *accueilNavSB = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FicheAlerteTableViewController *vc = [accueilNavSB instantiateViewControllerWithIdentifier:#"ficheAlerte"];
NSString *libelle = #"";
[vc initWithLib:libelle];
[self.navigationController showViewController:vc sender:self];
}
I already have relinked the field from the storyboard to my class, and in debug mode my UITextField is not nil.
My textfields are in a static UITableView, so I can see if the update worked scrolling the cell off the screen then scrolling it on.
Does anyone know why is this happening and how to solve the problem?
There is one error in your code. You are not using the correct attribute to load the text into the label.Use This.
This worked for me. I tested this on my Xcode.
-(void) editingChanged:(id)sender {
//label1.text = field.text;
libelleTemp=field.text;
NSLog(#"text is %#",libelleTemp);
// libelleTemp is set to #"" whatever the user has typed in when I'm in insert mode
}
You are using a UITextField, which can use the UITextFieldDelegate to register editing events and such. See Apple's documentation for more information.
I believe the method you are looking for is:
<UITextFieldDelegate> //link your textField.delegate = self
- (void)textFieldDidEndEditing:(UITextField *)textField {
libelleTemp = textField.text;
}
Another thing you could try is directly linking an (IBAction) method instead of setting the target programatically. Just control drag to link this method to your text field in IB.
- (IBAction)libelleEndedEditing:(UITextField *)sender {
libelleTemp = sender.text;
}
You are using the wrong action. UITextField's 'text' property will only be set once the editing has ended. So you need to wire your method to the "Editing Ended" event in Interface Builder.
I have a UITableView to which i've assigned a UITextField to each cell. I want to be able to accept input from each text field and dismiss the keyboard when the user taps anywhere on the screen other than the keyboard. This is the code I have so far, but I find the keyboard only gets dismissed when im on the last cell in the table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.gradesTableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
self.tf = [[UITextField alloc] initWithFrame:CGRectMake(225, (cell.contentView.bounds.size.height-30)/2, 50, 30)];
[self.tf setDelegate: self];
self.tf.tag = indexPath.row;
self.tf.textAlignment = NSTextAlignmentCenter;
self.tf.placeholder = #"0";
self.tf.backgroundColor = [UIColor grayColor];
self.tf.borderStyle = UITextBorderStyleRoundedRect;
self.tf.keyboardType = UIKeyboardTypeDecimalPad;
[cell addSubview:self.tf];
cell.textLabel.text = [self.adderArrayLabels objectAtIndex:indexPath.section];
return cell;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
self.tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap)];
[self.view addGestureRecognizer:self.tapGR];
NSLog(#"Started editing");
}
Ive tried both endEditing: and resignFirstResponder but both only dismiss the keyboard when im on the textfield in the last cell.
- (void)tap {
[self.tf endEditing:YES];
//[self.tf resignFirstResponder];
NSLog(#"tap called");
self.tapGR.enabled = NO;
}
With the NSLog statements in the code I can confirm the method tap is called every time the appropriate tap gesture is recognized but still the keyboard stays. How do I fix this?
The problem is here:
self.tf
Your class has a text field property, and every time you create a new text field, you assign it to this property. Then, you only try to endEditing: or resignFirstResponder on this property, which will always be the text field on the cell most recently created.
You don't need this property at all and can just use a local text field variable when creating the cells.
Then change your tap method to this:
- (void)tap {
[self.view endEditing:YES];
NSLog(#"tap called");
self.tapGR.enabled = NO;
}
And truly, the method should probably be: - (void)tap:(id)sender;
Also, as I commented, the gesture recognizer should be added in viewDidLoad. We only need to add it once, not each and every time a text field begins editing. The only reason to add it every time a text field begins editing is if you're also removing it every time the text field ends editing... but as the method that the gesture calls simply gets rid of the keyboard, I see no reason to do that.
In my iPad application i have few UITableView on same view created programmatically. They must pretend one multicolumn table. Each cell contains UITextField. It's size is equal to cell's size (its the reason why i cant get UITableView's delegate methods didSelect/didDeselect row). My problem is when i begin edit one text field then try to remove focus to other textfield it needs two taps. After first tap no one of cell is not editing. Such behavior observing only inside same table. If i want to change focus to other table its possible in one tap. What i missed?
Here is my UITextField Delegate methods in Cell's class
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([_delegate checkForAccess:self]) {
if (!((CTKTextField *)textField).isEditable)
{
[_delegate callPickerUnderCell:self];
return NO;
}
else
{
[_delegate getPosition:self];
return YES;
}
}
else return NO;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (_textField.text.length == -1)
{
_textField.rightView = nil;
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *input = textField.text;
[_delegate saveEdit:self withText:input];
}
If textFieldShouldBeginEditing is called after first tap and you return YES then text field starts editing. Probably you are making endEditing to the whole tableView after that.
If you call endEditing on the specific cell in your cell's delegate instead of the whole table view it should work.
That might be caused by autocapitalizationType. Your have to dismiss the auto correction before getting your textField becomeFirstResponder. Disable it will solve the problem.
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
Edit:
I was confused, try this one:
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Try to set UITableViewCellSelectionStyleNone to your table view cells with text fields.
I have a UITableView which I want to work in a similar way to the Contacts app in that there's an edit button which when clicked transforms the cells into edit cells.
At the moment they are set up using the cell style 'left detail' and I have overridden the setEditing method ready for implementation but I don't know how to transform the cells.
Some other answers on here included "Monitor when the table view's editing property changes (when the Edit button is pressed). Then add code to your delegate methods to compose, draw and indent cells in a different way, when the table view is in editing mode." which is exactly what I want but don't know how to do.
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:NO];
if (flag == YES){
// Change views to edit mode.
self.textField = [[UITextField alloc] initWithFrame:[_titleLabel frame]];
[self.textField setText:_titleLabel.text];
[self.view addSubview:self.textField];
}
else {
// Save the changes if needed and change the views to noneditable.
[_titleLabel setText:self.textField.text];
[self.textField removeFromSuperview];
}
}
In my method I have code taken from another question which works.. sort of (it makes a new editable text field on the fly in the wrong place and doesn't hide the label).
The apple guidelines aren't specific enough for me to understand how to develop the views.
In a nutshell, the way this works is you set an edit flag on the entire UITableView and then you implement a couple of methods (canEditRowAtIndexPath,commitEditingStyle) declared in the UITableViewDataSource protocol that determine which cells are being edited.
So first you need to put the UITableVIew into edit mode. You want to do that in the handler for your toolbar button:
[self.tableView setIsEditing:YES animated:NO];
Then, the tableview will call canEditRowAtIndexPath to determine if the row can be edited :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Finally, when the user is done editing, this method gets called:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
There is another example here:
http://www.behindtechlines.com/2012/06/02/enabling-configuring-uitableview-edit-mode/
I have a workaround.
If I create a custom row and make it look the same as the 'left detail' style, but using a textview on the right instead of a label, I can change the 'seteditable' and 'setenabled' fields of the views so that on edit they allow editing. I have hacked the font color so it changes when edit is clicked so the user can see that it is now editable.
This seems very messy - so I'm still looking for the best way to do this.
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:NO];
if (flag == YES){
[self.tableView setEditing:YES animated:NO];
[self.sampleLabel setEnabled:YES];
[self.sampleLabel setTextColor:[UIColor blackColor]];
}
else {
[self.sampleLabel setEnabled:NO];
[self.sampleLabel setTextColor:[UIColor darkGrayColor]];
}
}
- (void)configureView
{
self.titleLabel.text = [[self.detailItem valueForKey:#"title"] description];
self.ticketNumberLabel.text = [[self.detailItem valueForKey:#"reference"] description];
self.detailsLabel .text = [[self.detailItem valueForKey:#"details"] description];
self.sampleLabel.text = [[self.detailItem valueForKey:#"reference"] description];
// initially set labels to not be editable
[self.detailsLabel setEditable:NO];
[self.sampleLabel setEnabled:NO];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
// item can't be deleted now
return NO;
}