I am trying to set up undo and redo for each textfield and unsure how to figure out how to determine which text field is the first responder.
Is there an argument I can pass into the methods called by the buttons from the toolbar, or do I need to do some fancy footwork?
This is an idea:
If the viewController becomes delegate of each textField, then the viewController will get notified as each textField's value changes, or becomes first responder.
To adopt the delegation, you will do:
#interface MyViewController : UIViewController <UITextFieldDelegate>
#end
#implementation
- (void)someMethod{
// for a series of textfields
myTextfield1.delegate = self;
myTextfield1.delegate = self;
// or you hook the delegate in IB
}
// then you get notified
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// textField here that gets passed in as an argument is the first responder
// if you have, let's say tag number for each
NSInteger activeTextFieldTag = textField.tag;
}
#end
Here is the reference to UITextFieldDelegate Protocol
Related
Is there any sort of UIResponder Notification sent from objects that become a first responder? Or any way to know whether any UITextField or UITextView gets keyboard focus.
I have a ton of UITextFields/UItextViews in my application, and would like to add an input accessory view to all of them, but was hoping to avoid individually adding it in code to all their locations.
You can get your view controller to be a delegate of text views and text fields.
#interface MyViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate>
And then set it (in code or in the storyboard)
self.textField.delegate = self;
self.textView.delegate = self;
Then add these methods to your view controller
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
}
These will be called when the user selects the textField/textView.
I've put together a text field that can take text and save it and output it into a label on the same controller page. My question is how do I use another controller page (using tab view controller) and output the same text on the second controller.
I've linked the label as the same IBOulet that the text is saved as.
Below is my code for the firstcontroller.m
#import "FirstViewController.h"
#interface FirstViewController ()
#property (nonatomic, strong) IBOutlet UILabel* label;
#end
#implementation FirstViewController
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.label.text = #"";
return TRUE;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
self.label.text = textField.text;
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
textField.text = #"";
}
Thanks in advance
You can use the UITabBarDelegate to listen when the user click on another tab to pass the text.
Please check this link Class reference
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Or you can check this too Class reference
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
With this you could get the new view controller to assign the value in a good way.
Let me know if it helps you
Really, a UITextField is not a place to store data for any significant amount of time, as Model information should not be stored in a UIView. If you'd like this text to be accessible from multiple UIViewControllers, you should create a Model object that can store the string, and persist regardless of what happens with your view controllers.
You can pass this text to the model object by having your UIViewController be a delegate for the UITextField. The UIViewController should then be made to conform to <UITextFieldDelegate> so that you can listen for :
- (void)textFieldDidEndEditing:(UITextField *)textField
When this is called, pass the text to your model. The model could be set up with a Singleton pattern, just have 1 static model object accessible globally. When your app exits, you could write this object to disk using <NSCoding>, so that you can read it back in again next time the app starts.
For non-trivial data, I'd recommend using CoreData.
tl;dr- dont pass text to other controllers directly from the textfield.
I have few text fields in a UIViewController. For some of the text fields I have used IBOutlet to make property. For some textFields that I add programmatically (since they are in scroll view, they are not in the constraints of the view controller window in soryboard. ) , I have just made them property without IBOutlet.
So for example, I have:
#property (weak, nonatomic) **IBOutlet** UITextField *descriptionTextBox;
#property(strong , nonatomic) UITextField *cityTextField;
Now I set the delegates of both in ViewDidLoad & also in .h file <UITextFieldDelegate>
But after implementing - (BOOL)textFieldShouldReturn:(UITextField *)textField method , the keyboard only return for the text field having IBOutlet.What can I do for it?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; //works only for text field declared in storyBoard.
return YES;
}
In your view controller, say [self.view endEditing:YES]. This will dismiss the keyboard regardless of who is first responder.
I think it would be helpful to see more code.
However your issue could be that you're not setting the delegate on your UITextFields hence the method only being called on the one in the storyboard.
Check to see whether you have set delegate to your UITextField objects that were created programatically.
I'm trying to move the view up when the keyboard shows so it wont cover up the screen, but for some reason its the -(void)DidBeginEditing: (UITextField *)textfield is not working.
- (void)textFieldDidBeginEditing:(UITextField *)ga1
{
/* should move views */
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 220);
}
- (void)textFieldDidEndEditing:(UITextField *)ga1
{
/* should move views */
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 220);
}
its nor going into the method, can anyone tell me why?
In the interface of the class add the line , so in the .m file you would put above where it says #implementation...
#interface MyClassName () <UITextFieldDelegate>
// properties can also go here
// for example dragging the IBOutlet for the textfield from the storyboard
#end
You then in viewDidLoad should set the delegate of the UITextField like so...
-(void)viewDidLoad {
// whatever code
self.textField.delegate = self;
}
Alternatively, and more cleanly, you can do this in the story board by control clicking the text field and dragging the indicator to the view controller class icon (the icon to the furthest left) in the lower bar.
Also, why are you calling the argument to the textField in your implementation "ga1"? Best practice you should call it
- (void)textFieldDidBeginEditing:(UITextField *)textField
One final note is that if you have multiple textFields you should set the delegate for each of them in the way described above. This is why the storyboard way of doing it is "cleaner," because it keeps you from having multiple delegate declarations in your code.
If implemented, it will get called in place of textFieldDidEndEditing
#interface MyViewController :UIVieController <UITextFieldDelegate>
#property UITextField *myTextField
#end
#implementation MyViewController{
}
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextfield.delegate=self;
}
-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
if(reason==UITextFieldDidEndEditingReasonCommitted)
{
NSLog(#"Committed");
}
}
Implement <UITextFieldDelegate> Protocol for your class. And set the delegate to self. Here is Apple's document UITextFieldDelegate Protocol
I'm making an application that requires the use of multiple textFields with number pads as there first responder. I have created an image to use as a negative button that will be an addition to the number pad.
I am wondering if there is a way to check which textField the number pad is typing to.
Any help would be appreciated!
If all of your potential first responders are UITextFields, another approach would be to conform your controller class to UITextFieldDelegate protocol, and then grab a reference to the currently editing UITextView at the time it begins editing.
Conform your class in your .h:
MyController : NSObject <UITextFieldDelegate> //Might often be a UIViewController rather than an NSObject subclass...
Define a property:
#property (weak, nonatomic) UITextField *editingField;
Then synthesize in your .m:
#synthesize editingField = __editingField
Then implement:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setEditingField:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setEditingField:nil];
}
Now, whenever you want to know which text field is your first responder:
UITextField *firstResponder = [self editingField];
If there are only a limited number of them, you can query each with [textFieldN isFirstResponder]. If you want a general purpose utility, you can look at each subview in a view and see whether it is the first responder or whether any of its subviews farther down the hierarchy are.