How can i gather sent events from a UITextField in one place or am i forced to create outlets and actions for every single event i intend to use?
If I got your question correctly you need to get the text from UITextField when this UITextField loose focus (user taps elsewhere). To achieve this you need:
Declare your class as (in yourClassName.h
file)
Implement in yourClassName.m file this method:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *someStringOrWhateverYouNeed = textField.text;
}
Any time user will press return button on keyboard your class will have a notification and call this method.
In case if you need to gather event from multiple UITextFields you can mark all your textField with specific tags and create one IBAction like this:
- (IBAction)getTextFieldEvent:(id)sender {
UITextField *currentTextField = (UITextField *)sender;
switch (currentTextField.tag) {
case 1:
// some code here for textField with tag = 1
break;
case 2:
// some code here for textField with tag = 2
break;
case 3:
// some code here for textField with tag = 3
break;
default:
// some default code here
break;
}
}
For different event types I can suggest to create different IBAction's. If you do not need to change UITextField's properties (e.g. font etc.) then you do not really need IBOutlets.
Hope that helps :)
Do another IBAction connection with the same name. Then erase the duplicate method. Both textfields will be connected to the same IBAction method.
Related
I have three buttons with different actions.Now I don't want to create three IBAction to my buttons.In single IBAction Method can i write the actions for those three buttons.
I am new to Xcode,Can anyone help me to do this...
Thanks in Advance....
try like this
in . h file
#property (strong, nonatomic) IBOutlet UIButton *yourbutton;
in .m
#synthesize yourbutton;
- (IBAction)yourClicked:(id)sender {
UIButton *resultebutton= (UIButton*)sender;
NSString *buttontitle=resultButton.currentTitle;
if ([buttontitle isEqual:#"firstBtitle"]) {
// perform your 1st button action
//call your method
}
else if ([buttontitle isEqual:#"secondBtitle"]) {
// perform your 2nd button action
}
else if ([buttontitle isEqual:#"thirdBtitle"]) {
// perform your 3rd button action
}
}
Assign tag for buttons, and in IBAction method, check Button tag and do action, according to tag of button.
Please correct me, if I get you wrong:
You have three buttons and you want them to trigger the same IBAction. The IBAction itself decides what to do based on which button calls it.
This sounds to me like a perfect example for the "sender" parameter.
Create something like this:
- (IBAction)doSomeAction:(id)sender
{
if ([sender isEqual:self.buttonOne]) {
NSLog(#"ButtonOne");
} else if ([sender isEqual:self.buttonTwo]) {
NSLog(#"ButtonTwo");
} else if ([sender isEqual:self.buttonThree]) {
NSLog(#"ButtonThree");
}
}
With the sender you can identify the button, which calls this method. This way, you can avoid handle tags which can be very annoying to use.
Make sure you connect all three buttons to this action - take a look at the connections inspector. This is very important and a common source for errors. If you remove any connection to an outlet or an IBAction, also check, if this connection ist remove in the Storyboard-object.
If everything is in place just compare the sender with the outlets of the buttons.
Step 1:
Assign your all three buttons different tag in storyboard/XIB,
For ex. firstButton with tag=1, secondButton with tag=2 and thirdButton with tag=3
Step 2:
Define your method like this and bind all your buttons with this method
- (IBAction)buttonAction:(UIButton *)sender
{
if (sender.tag==1) {
NSLog(#"First Button");
} else if (sender.tag==2) {
NSLog(#"Second Button");
} else if (sender.tag==3) {
NSLog(#"Third Button");
}
}
And your work is done.
I am developing a custom keyboard using app extension in iOS 8.
I have a return key, and on press I want to check if input object is an UITextView, then move to next line using the following code: [self.textDocumentProxy insertText:#"\n"];. And if the input object is an UITextField, then dismiss the keyboard this way: [self dismissKeyboard];
Something like this:
- (void) returnKeyPressed
{
if (inputObjectIsTextView)
{
[self.textDocumentProxy insertText:#"\n"];
}
else if (inputObjectIsTextField)
{
[self dismissKeyboard];
}
}
The question is: How can I detect what kind of input view is currently editing?
There is no need to detect input view is a UITextField or UITextView to enter a new line
as described here under heading "API Quick Start for Custom Keyboards"
says
[self.textDocumentProxy insertText:#"\n"]; // In a text view, inserts a newline character at the insertion point
That means no need of detection, You can do in this way
- (void) returnKeyPressed
{
[self.textDocumentProxy insertText:#"\n"];
}
This will execute if inputView is UITextView and not execute if input type is UITextField.
I have create a keyboard and test this before post it here.
You can`t do that , if you look at the documentation ,you see that there is no public API for detecting keyboard Extensions Text Input Object.
From The Doc.
Because a custom keyboard can draw only within the primary view of its UIInputViewController object, it cannot select text. Text selection is under the control of the app that is using the keyboard. If that app provides an editing menu interface (such as for Cut, Copy, and Paste), the keyboard has no access to it
I think this also implies that you cannot access text Input object from the app that are using your keyboard extension.
Try something like this, take a reference of active view as 'inputViewObject', then try
- (void) returnKeyPressed
{
if (inputViewObject.class==[UITextView class])
{
[self.textDocumentProxy insertText:#"\n"];
}
else if (inputViewObject.class==[UITextField class])
{
[self dismissKeyboard];
}
}
Hope it helps.
I have two text fields in my interface. txtUser allows the user to enter their username and txtPass allows the user to enter their password. I have programmatically assigned tags in the viewDidLoad method. I use these tags in order to identify the different text fields. The issue is that the textFieldShouldBeginEditing method only recognizes txtPass, while ignoring the txtUser. Both of these variables are linked to their respective text fields, so that's not what's causing the issue. Below is my code:
- (void)viewDidLoad{
[super viewDidLoad];
self.txtUser.tag=10;
self.txtPass.tag=20;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 10){
self.btnNext.enabled = YES;
self.btnPrevious.enabled = NO;
}else if(textField.tag == 20){
self.btnNext.enabled = NO;
self.btnPrevious.enabled = YES;
}
return YES;
}
Are you setting the delegate for the text fields anywhere?
self.txtUser.delegate=self;
self.txtPass.delegate=self;
Also make sure you implement UITextFieldDelegate on your view controller.
#interface YourViewController: UIViewController<UITextFieldDelegate>
My semi-confident guess is that you've setup the delegate, but not the outlets, so the code that you think is assigning the tags is not working (assigning .tag = 10 to nil).
The incoming tag is then zero (the default) for both fields, so the else branch in the delegate code is always running.
Fix by setting the outlets (or by setting the tags in IB).
I'd like to achieve the following behaviour of a single UITextField and the keyboard:
when the view has loaded UITextField becomes first responder and the keyboard opens (so far so good):
- (void)viewDidLoad
{
[self.editField becomeFirstResponder];
self.editField.delegate = self;
}
Now user inputs some text and presses the return key. That text is added to the data source in the following method:
- (IBAction)didEndOnExit:(id)sender {
//add self.editField.text to data source
}
Now after the return key is pressed and the above UITextField's method gets called and executed I would like the UITextField to clear, the cursor to be placed and be visible at the beginning of the text field and the keyboard not to hide so that new item could be entered in the textfield and added to the data source.
This is how I return to the previous view in the app (using a button):
- (IBAction)backButtonPressed:(id)sender {
[self dismissViewControllerAnimated:NO completion:^{}];
}
I tried playing with UITextFieldDelegate's methods and UITextField's becomeFirstResponder and resign FirstResponder, but am unable to achieve the above described behaviour. I've seen posts here on Stack Overflow about using consecutive UITextField's to enter data, but not to use the same UITextField time and again.
You should override the below mentioned method and do whatever you want and showing the keyboard too.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Have a try :)
While dismissing view controller all other code executions freezes when (view controller is being dismissed).
Use some delay (performselectorafterdelay). or
completionhandler of dismissingviewcontroller for executing your code when viewcontroller is being dismissing and
Also execute code on main Thread which includes UI changes or updates
after saved your data.. you have to make the uitextfield to be nil... like self.editField.text = nil. then you can call [self.editfield becomesfirstresponder].. after you will add more items to array or else anything everytime the field should be nil after you added..
From the Doc
Return Value: YES if the text field should implement its default
behavior for the return button; otherwise, NO.
For EX - Use the Delegate of UITextField :
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//add self.editField.text to data source
textField.text = #"";
return NO; // This will do your need
}
Because I used UITextField's action method - (IBAction)didEndOnExit:(id)sender the keyboard always got closed. Apparently that method makes the UITextField resign from being first responder. Instead of adding data to data source in that method, it can be added in the delegates's method - (BOOL)textFieldShouldReturn:(UITextField *)textField and so the keyboard won't close.
I know this is a basic question but i´m a little confused, so i hope you can help me. I have a tableview with multiple dynamic tableview cells, and inside each tableviewcell i have multiple textfields. Each cell has a different tag and also the textfields and i want to access the uitextfields values as you can imagine. My problem is, i´m not using IBoutlet for the textfields (it would be a enormous amount of IBoutlets)...I´m using - (void)textFieldDidEndEditing:(UITextField *)textField...but i just can´t seem to make the correct connections in the IB, this is my code:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102]) {
[textField resignFirstResponder];
}
After this, do i have to connect the respective UItexfield (and all textfields) to self? and then, do i have to use the editing did end event?...
Regards
I guess that the answer to this question is another question: What do you want to do with the text that the user enters?
I assume that you have some kind of data model that you want to store the data in.
If so, then when this function is called, you need to take the text that is already in the textField and save it to your data model immediately as it is entered.
For instance, you can access the text that was entered like this:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102])
{
[textField resignFirstResponder];
yourDataModel.stringToSave = textField.text;
}
}