TextField clear button - objective-c

I have 5 textField popovers that are all dependant on each other. The value in the first field sets what will show in popover for second field and so on...
If the user removes one of the fields, I want to be able to clear all fields following that are linked to this field. I started by using the textFieldShouldClear method, but I can't seem to figure out how to tell it which textField is being cleared. It either clears everything, or doesn't clear at all.
Thanks

It sounds like your textFields are set up in IB, which means you can declare 5 IBOutlet UITextField objects in your header file and hook these up to the fields in IB. Then in your delegate method, you can do something like this:
- (BOOL)textFieldShouldClear:(UITextField *)textField {
if([textField isEqual:myField1]) {
myField2.text=#"";
myField3.text=#"";
...
}
else if([textField isEqual:myField2]) {
myField3.text=#"";
...
}
//etc....
return YES;
}
Hope this helps!

Related

Enabling Done Button When User Changes Values - Xcode

I am trying to make it to where if any value is typed on the keyboard without hitting the return key and two of the labels have been changed from "Select" to something else, the button up at the top becomes enabled. However, I have tried using an IBAction saying:
- (IBAction)valuesChanged {
if (textField.text != nil && ![labelOne.text isEqualToString:#"Select"] && ![labelTwo.text isEqualToString:#"Select"]) {
NSLog(#"Success");
}
else {
NSLog(#"No Success");
}
}
But I have realized that this does not work because:
The textfield does not work when I put the IBAction Sent Event as "Value Changed"
The labels won't accept an action.
How do I go about doing this?
The text field delegate method that tells you that the user is typing a character in the text field or otherwise changing its contents is textField:shouldChangeCharactersInRange:replacementString:. Implement it in your text field delegate and respond as appropriate. You will also just return YES.
One reason your original code couldn't be hooked up might be that you have not used the canonical form of an IBAction method; it should be
- (IBAction)valuesChanged:(id)sender {
Another problem in your original code is that a UITextField does not emit Value Changed. What you wanted was Editing Changed. But the delegate method works just as well.
Try UITextField delegate methods.
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;

Xcode - setFocus on a text field, becomeFirstResponder isn't enough

At the moment, I trigger a method on 'Did End On Exit' in my app (I'm aware that this may not be the greatest way of doing it but I'm very new to Objective C and Xcode for that matter and I'm simply doing what feels comfortable to me).
This method resigns the firstResponder from the current text field and applies it to a later text field.
The problem I'm facing is that the keyboard covers the next text field so that the use has no idea where the focus is and therefore what they are required to type.
How do I get it so that my keyboard shifts down and actually shows the text box that is currently active? Making something the firstResponder simply doesn't do what I want it to, unless there's part of the implementation I'm missing.
Here's my simple method:
- (IBAction)firstNameNext:(id)sender {
[firstNameTextField resignFirstResponder];
[surnameTextField becomeFirstResponder];
}
Any advice would be super.
Add UIScrollView in your main view then all contents as subview to UIScrollView
Now when specific UITextField needs to be able to visible in view use its delegate like this:
Note: add UITextFieldDelegate in .h file like this
#interface yourViewController : UIViewController<UITextFieldDelegate>
Also bind with File's Owner
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
if(textField == yourSpecficTextField) //one u want move upwards
{
yourScrollView.contentOffset = CGPointMake(0,200); //required offset
}
... //provide contentOffSet those who needed
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
yourScrollView.contentOffset = CGPointMake(0,0); //make UIScrollView as it was before
}
If you have keyboard input fields that will be covered by the virtual keyboard, then you need to move those fields out from under the virtual keyboard.
The normal way to do this is to have the controller's view be a scrollable view like UIScrollView. Moving Content That Is Located Under the Keyboard gives a very robust way of adjusting your scroll view and ensuring the required field shows.

UITextView won't resignFirstResponder so another can becomeFirstResponder

I have a toolbar in my UITextfields' inputAccessoryView. If the 'next' button is hit it makes the next textfield in an orderedSet of all my textfields become the first responder. THAT works perfectly.
BUT I can't get the 'previous' textfield to becomeFirstResponder.
I've checked in the console and the textfield does call textFieldShouldBeginEditing and I am returning YES but it never calls textFieldDidBeginEditing and the textfield that should resignFirstResponder never calls textFieldShouldEndEditing.
So the textfield is getting the message to becomeFirstResponder but doesn't.
- (IBAction)keyboardBarButtonDidTouch:(UIBarButtonItem *)sender
{
if (sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
{
[(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue-1)] becomeFirstResponder];
}
if (sender==self.nextBarButton&&self.activeTextFieldArrayIndex.intValue<(self.textfields.count-1))
{
[(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue+1)] becomeFirstResponder];
}
if (sender==self.doneBarButton)
{
[self.activeTextField resignFirstResponder];
}
}
The weirdest thing is that if I force the textfield to resignFirstResponder by some action that just does that, the 'previous' textfield suddenly becomesFirstResponder. Like it was being added to a list of responders and it became its turn....
And as is often the case when strange things happen, it was unrelated to becoming first responder. I was accidentally changing my activeTextfield pointer to nil in the middle of a bunch of logic by way of the textfield delegate. ^o^
Disregard!
1) Try making your code less error prone.
instead of this
if(sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
do this
if ((sender==self.previousBarButton) && (self.activeTextFieldArrayIndex.intValue>0))
2) use textfield.tag property and set unique tags for these text fields. and do your stuff about become/resign first reponders by using
(uitextfield *)field[self viewWithTag: uniqueTag];

tableview with text fields in each row having resignkeypad issue

i am adding the textfield on each row inside the cellforrow method of tableview,but when i type anything on any textfield ind try to resign textfield on view touch, but it only works for last row's textfield only. what is the solution for that issue,
please help.
Thanks
Dhananjay
if i understand you correctly this will probably work:
add the protocol to your viewController in question and add
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
// custom code
}
afterwards make sure that each textField has your viewController as delegate.

Select text (content) instead of cell with NSCell

I'm currently working in a project with a NSOutlineView...
I use, of course, NSCell(s) and I need to let the ability to select text inside the cell...
Or at least... prevent the selection (and highlight) of the cells...
I search all options on IB, but can't found the right one...
Is there a way, programmatically or not, to prevent selection/highlighting of cell, nor let user select cell content ?
Thanks =)
That's not much NSCell related, maybe you're looking to implementing outlineView:shouldSelectItem: in your delegate.
On the NSCell, setEnabled:NO, may help too. From the documentation:
setEnabled:(BOOL)flag
The text of disabled cells is changed to gray. If a cell is disabled, it cannot be highlighted, does not support mouse tracking (and thus cannot participate in target/action functionality), and cannot be edited. However, you can still alter many attributes of a disabled cell programmatically. (The setState: method, for instance, still works.)
Try setting:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
You could also try overriding highlightSelectionInClipRect:, but I'm not totally sure this will work.
Let's take a quick example like the outline view below. There are 3 columns: firstName, lastName, and fullName.
In this particular example, let's say we want to only allow firstName and lastName to be editable while fullName (which is potentially derived from firstName and lastName) is not. You could set this up in Interface Builder by checking or unchecking the editable checkbox for the table column. To do that, click 3 times on one of the table columns (not the header, but inside the outline view); this first selects the NSScrollView, then the NSOutlineView, then an NSTableColumn:
You'd set the attributes like the following:
That provides a start by setting a default editable value for the entire column. If you need more control over whether a particular row's item value should be editable or not, you can use the outlineView:shouldEditTableColumn:item: delegate method:
#pragma mark -
#pragma mark <NSOutlineViewDelegate>
- (BOOL)outlineView:(NSOutlineView *)anOutlineView
shouldEditTableColumn:(NSTableColumn *)tableColumn
item:(id)item {
if ([[tableColumn identifier] isEqualToString:#"firstName"] ||
[[tableColumn identifier] isEqualToString:#"lastName"]) {
return YES;
} else if ([[tableColumn identifier] isEqualToString:#"fullName"]) {
return NO;
}
return YES;
}
If you want to control whether a particular row in the outline view is selectable (for example, you could prevent selection of a group item), you can use outlineView:shouldSelectItem:.
- (BOOL)outlineView:(NSOutlineView *)anOutlineView shouldSelectItem:(id)item {
// if self knows whether it should be selected
// call its fictional isItemSelectable:method:
if ([self isItemSelectable:item]) {
return YES;
}
/* if the item itself knows know whether it should be selectable
call the item's fictional isSelectable method. Here we
are assuming that all items are of a fictional
MDModelItem class and we cast `item` to (MDModelItem *)
to prevent compiler warning */
if ([(MDModelItem *)item isSelectable]) {
return YES;
}
return NO;
}