How to get UITextField from inputAccessoryView button

I have a dynamic UITextFields number that everything has inputAccessoryView . It consists of a UIToolbar on which there is a button. When this button is pressed, it calls the method, however in this method I somehow need the ability to access the base UITextField .

Please can anyone advise how this is possible?

 // Create the toolbar controls UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(navigationBarDoneButtonPressed:)]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; // Setup the toolbar navigationToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)]; [navigationToolbar setItems:@[flexibleSpace, doneButton]]; // In a UITableView I create a number of cells which contain UITextFields.... // This is the method that gets called when a user presses the done button - (void)navigationBarDoneButtonPressed:(id)sender { NSLog(@"Based on sender I need to access the underlying UITextField."); } 
+4
source share
2 answers

You can track the current focus of a UITextField in a variable and use this variable in the inputAccesoryView method:

in your .h file: make sure you comply with the UITextFieldDelegate protocol:

 @interface MyViewController : UIViewController <UITextFieldDelegate> 

and add this property:

 @property (assign, nonatomic) UITextField *activeTextField; 

In your .m file: when creating dynamic text fields:

 ... theTextField.delegate=self; ... 

And add these implementations of the UITextFieldDelegate protocol:

 - (void)textFieldDidBeginEditing:(UITextField *)textField { self.activeTextField = textField; } - (void)textFieldDidEndEditing:(UITextField *)textField { self.activeTextField = nil; } 

And now, in the method called by your inputAccesoryView :

 - (void)navigationBarDoneButtonPressed:(id)sender{//Just an example self.activeTextField.text=@ "Test"; } 
+6
source

I would subclass UITableViewCell with a delegate like

MyTextEditTableViewCell.h

 @class MyTextEditTableViewCell; @protocol MyTextEditTableViewCellDelegate <NSObject> - (void)didDoneButtonPressed:(MyTextEditTableViewCell *)sender; @end @interface MyTextEditTableViewCell : UITableViewCell @property (nonatomic, weak) id<MyTextEditTableViewCellDelegate> delegate; @property (nonatomic, strong) UITextField *textField; @end 

MyTextEditTableViewCell.m

 @implementation MyTextEditTableViewCell - (void)navigationBarDoneButtonPressed:(id)sender { // Call delegate if (self.delegate) [self.delegate didDoneButtonPressed:self]; } @end 

Subclass of UIViewController (or UITableViewController)

 ... - (void)didDoneButtonPressed:(MyTextEditTableViewCell *)sender { UITextField *cellTextField = sender.textField; // do some stuff with text field } 

Remember to set the delegate to the cell when you configure it.

0
source

Source: https://habr.com/ru/post/1498638/


All Articles