TextField on Custom Cells - objective-c

I am using custom cell inside tableView. In that custom cell, there is label and textField....this custom cell is being reused to display ten items. I actually want to open a datepicker on one of the cell's text field but I am not able to understand that how this work can be achieved?

All you need to do is to create an instance of UIDatePicker and set it as inputView of one of your text fields. When the text field is tapped UIKit will animate appearance of the picker for you. In turn, you'll have to handle user's input and update the text field's text.

Please set the text field tag and delegate in cellForRowAtIndexPath method...
like..
cell.yourTextFieldName.tag = 1000;
cell.yourTextFieldName.delegate = self;
In .h file add the UITextFieldDelegate
Then in the textFieldShouldBeginEditing delegate you can get the picker.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSInteger txtFldTag = textField.tag;
if(txtFldTag == 1000){
//write the method to show picker.
return NO;
}

Related

How to call a method when values has been entered in text box

In my app, i have a text box and handheld scanner. when i scanned something, the barcode values comes in text box. I don't need any special code to get the bar code value inside the text box. Till here i am fine. But now when i have a value in textbox , i want to call a method which will further do some changes in Core data. I have the code to make changes into core data for the row with value = value in text box.
But i don't know how to call this method or how do i know that values been entered in text box and i can call the method.
Please help me in doing that.
Sometimes handheld device add return character to the end and if yours do that add delegate to the text field
self.textField.delegate = self;
and see is that method called:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"textFieldDidEndEditing method called");
}
If this method is not called add notification to the text field which will be called when the text changed:
[self.textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
and add method which will be called:
-(void)textFieldDidChange(id)sender
{
NSLog(#"textFieldDidChange method called");
}
You need to create an IBOutlet variable and link it to your textbox.
#interface YourClassOrController
{
IBOutlet NSTextField *myTextField;
}
...
#end
In interface Builder, you need to Ctrl-Drag to link your textfield to your variable.
Finally in your code you use
NSString code = [myTextField stringValue];

Two classes reference each other / access UITableViewCell from UITextField inside it's delegate methods?

I have a custom UITableViewCell and a custom UITextField, with the text field inside the table view cell.
I need to do some validation on text entered in the field, and display a little tick or cross image in the cell depending on if the text entered was okay.
My table view cell class imports the text field's header, and declares a .textField property which I can use.
My text field class has a class extension (#class) of my table view cell, and declares a .cell property, which I'd like to be able to use to access the cell's image, and display / update it to the tick or cross.
The text field's delegate is my view controller, and I'm using textFieldDidEndEditing in the following way to attempt to set that image (validationConfirmation). However, it's not working, nothing's happening, no error, etc.
Any idea what I'm doing wrong? This is a puzzler.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
SignUpTextField *tf = (SignUpTextField *)textField;
if (textField.text.length < 6) {
tf.cell.validationConfirmation.hidden = NO;
tf.cell.validationConfirmation.image = [UIImage imageNamed:#"cross.png"];
}
}
So here, I'm getting the text field subclass (SignUpTextField, or I'm attempting to), and trying to set its cell property's image property. Is this wrong? Messy? Bad practise?
You don't need to (and you shouldn't) have a cell reference inside textField class. I believe your textField is added as a subview of the cell. Hence in the textFieldDidEndEditing: method, you can get the cell using textField.superview. Or alternately, you can assign a tag to the textField corresponding to the tableview row that it appears in and access the cell using cellForRowAtIndexPath: method of tableview where you can use the tag to create your indexpath.

How to get textFields in custom UITableView

Very beginner obj-c question.
I have plain UITableView with two sections, but I am interested only in first section now. This section have four custom cells (inherited from standard UITableViewCell), and they have a UITextField's as a property.
I need to improve custom Input Accessory View with buttons "Next", "Previous"(for switch between textFields in tableview) and "Done" (dismissimg of keyboard). http://uaimage.com/image/62f08045
In -textFieldShouldReturn i set tags for textFields from 0 to 3. My next plan is to add textFields into NSMutableArray in -viewDidLoad and then just set and resign first responder for the textFields. Approximate code listing for "Next" button:
- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view {
for (UITextField *textField in [self textFieldsArray]) {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
UITextField *field = [[self textFieldsArray] objectAtIndex:textField.tag + 1];
[field becomeFirstResponder];
}
}
}
Questions:
Is this a right way or maybe there is a better approach to solve problem?
Do I need to tag textfields or use indexPath of cells in what they are built in? (or what is the best to track textFields?)
And the main question: what is the syntax to "get" textField from cell?
Sorry for the dumb questions, I am a very beginner.
Thanks,
Alex.
I think you have the right idea, but a few things come to mind:
Just to be safe, don't start with tag number 0. Every view has a tag number defaulted to 0, so you may get something unexpected.
Don't set the text view's tags inside of the textFieldShouldReturn, set the tags in cellForRowAtIndexPath or viewDidLoad, wherever you init the textFields.
Add the textFields to the cell's contentView, not the cell itself.
You don't have to resign first responder from the first text field, you can just becomeFirstResponder on the new one.
Make sure you're handling the last text view edge case: You could loop around to the first text field or simply dismiss the keyboard at the end.
If you want to get the textField in the cell:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:ROW_NUMBER inSection:1]];
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:TEXT_FIELD_TAG];

UITableViewCell selection over UITextView

I get stuck with my cell selection. I have custom cell with UITextView object embedded in. It is not editable, not scrollable, with user interaction enabled. And I can not select cell over this view.
I've tried the same thing but with UITextField object and selection works.
Any ideas how to make it work?
This is the test project. There is just one table view with one cell. There is one text view and one text field in this cell. You can select the cell over the text field and can not over the text view.
Turn off "User Interaction Enabled" on your text view and the selection will work fine (MainStoryboard.storyboard->Table View Controller->Table View->Table View Section->Custom Table View Cell->Text View). Allowing user interaction on the text view is capturing the taps before the table view cell gets them. Since your text view isn't scrollable or editable, you don't want user interaction enabled.
Just to expound a bit on John Stephen's answer (which is the best as far as I can tell).
I've tried this by subclassing UITextView and overriding canBecomeFirstResponder:
#interface MyTextView : UITextView
#end
#implementation MyTextView
- (BOOL)canBecomeFirstResponder {
return NO;
}
#end
I've also set editable, selectable, and scrollEnabled to NO:
MyTextView *textView = [MyTextView new];
textView.editable = NO;
textView.selectable = NO;
textView.scrollEnabled = NO;
None of these worked. :(
The equivalent of John's answer for Storyboards for programmatic solution is:
textView.isUserInteractionEnabled = false

Text entry ios question

I have two UILabels in a view that display my band and song name as strings. I am working on also adding the option to change either of these strings manually. I want to keep it as is, and I've added 2 buttons to manually enter a song name or band name. The thing is, all the text editing as far as I understand it needs to have actual TextField or TextView to bring up the keyboard etc.
I just want to touch one button for "enter song name" and be given a keyboard and when enter is hit, replace the string in the uilabel with that string, and the same for enter band name uibutton, and change the uilabel again.
Problem is in the docs I don't really understand how to do this. Does anyone have an idea about text entry in ios and can give me a pointer/tip on how to do this?
Just Declare your label Global and implement in implementation method
Take all song and all album data in array
and display all data in table view
now by using Delegate method of
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
change the text of Label as u want
#interface songViewController ()
{
UILabel *bandLabel;
UILabel *songLabel;
NSArray *bandData;
NSArray *songData;
}
#end
#implementation songCreateViewController
- (void)viewDidLoad {
[super viewDidLoad];
{
bandLabel=[[UILabel alloc]init];
bandLabel.frame=CGRectMake(40, 580, 500, 30);
bandLabel.text=#"Join group as";
[self.view addSubview:bandLabel];
songLabel=[[UILabel alloc]init];
songLabel.frame=CGRectMake(40, 580, 500, 30);
songLabel.text=#"Join group as";
[self.view addSubview:songLabel];
}
#pragma mark Table view Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==albumTableView)
{
bandLabel.text=[bandData objectAtIndex:indexPath.row];
}
else
if(tableView==songTableView)
{
songLabel.text=[songData objectAtIndex:indexPath.row];
}
#end
What you are going to have to do is replace the label with a text field and set that as the first responder when the user presses the button. You will need a class that is the delegate of the text field so you handle enter in the textFieldShouldReturn method to resign first responder on the text field (to close the keyboard) and change the text field back to the label view and update its contents.
It can be achieve by many ways..
1) you can use hidden in your button methods to hide label and show textView.
2) you can use textField in place of label, make textfield.editable=NO and when button is pressed just clear the textfield by textfield.text=nil and make textfield editable.
Try these..
You want like when user touch on button you want to add text and button should be look like UILable. is it your objective?
If I am right you can achieve this thing to add gesture in uilable, you don't need to add button and handle tab method you can change the text.
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}