I have a UITableView with UITextView in all the cells. I do have a bar button .On editing any of the UITextView and when I tap on the bar button all the UITextViews data must be stored in the form of NSArray of dictionaries. I will be thank full to you ,If I get out of this problem
Don't wait for your bar button to be pushed. If the table view is scrolled and the cells go off screen then any text entered by the user and not already saved will be lost.
Instead, your controller should be the delegate for all of the text fields. Whenever the text changes, save the change (probably using textField:shouldChangeCharactersInRange:replacementString:) into a temporary array / dict. Now, when configuring your cells, enter the text from the temporary store into the cell text fields and when the bar button is pressed use your temporary store to save the entered text.
To get the text to save, assuming that the text field is tagged with the array index and that the array contains mutable dictionaries (you should consider adding some protection and error logging):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *stringToSave = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"Save me: %#", stringToSave);
NSMutableDictionary *storeDict = [self.storeArray objectAtIndex:textField.tag];
[storeDict setObject:stringToSave forKey:#"description"];
return YES;
}
Related
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];
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];
Im new to objC and Im currently experimenting on UISearchDisplayController. Basically I have an array of string as my data and I use UISearchBarDisplayController to filter my data. Im able to retrieve the correct values when I enter my searchText into the search bar. However, the tableView disappears when my searhBar text is empty.
Would it be possible to prevent the tableView from hiding in this such case. What I want is to just to display all the values in my array in the table if the searchBar text is empty.
I checked the hidden/alpha/frame property of the table and tried to fix my issue here but I think Im in the wrong path here. I'm thinking if i need to subclass the UISearchDisplayController and override the [setActive:YES animated:YES];? Any hint would be appreciated.
You should recieve an event for any change to the search parameters, including when the string is empty.
If you change your implementation of that delegate method to check if the string is empty, you can return the original data instead of the filtered data. This should achieve what you're asking for without the need for subclassing.
I ended up working with UISearchBar and UITable.
So here are the scenarios I encountered.
Display the the UITable when the searchBar is clicked.
UITable shows all the data from my plist when searchText is empty.
UITable shows filtered results from my plist that matches the searchText.
Dismiss the keyboard on press of the search button but don't disable the cancel button on the searchBar.
Remove the UITable when cancel button is press.
I don't have the animation for showing the table for now but this works for me. Also, I allowed the user interaction and scrolling on my table during search so no overlay needed in this case. Glad its working now. :)
I created a sample project for those who might need. Here is the link. Apologies for a messy code and leaks issues. Just posted this to share. :)
You could try by always leaving a zero-space width characters in the search textfield :
static NSString* zsp = #"\u200B";
//In the UISearchBarDelegate
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(searchBar.text.length == 1 && [text isEqualToString:#""])
{
searchBar.text = zsp;
return NO;
}
return YES;
}
I've a UITableViewController which has Edit/Save and cancel buttons. At present I'm displaying 5 row's of custom UITableViewCell's. Custom UITableViewCell contains textfield and label. Let's say if user enters text in 1st table cell and hits "Save" then I'm losing the entered/modified text. Whereas if user hits return key after entering/modifying text then the entered/modified value is captured via:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
.......code exists to capture value here
}
but on the other hand if user hits "Save" button without returning the textfield then I'm losing the entered/modified value. What's the best way to capture user's input here:
a) when user returns text field --> which I'm doing already
b) when user hits save button without returning textfield/textview --> ????
thanks in advance,
Rama
You could keep a property that holds the string as it's being entered using a delegate method. Something like:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSMutableString *testString = [NSMutableString stringWithString:self.textField.text];
[testString replaceCharactersInRange:range withString:string];
self.stringInProgress = testString;
return YES;
}
Another option might be to call [tableView endEditing] as the first thing to do on a save but I'm not certain that fires the textFieldShouldEndEditing: notification.
I have a NSTextField on the view of an NSCollectionViewItem.
When you click an NSCollectionViewItem the item is selected.
When you click the NSTextField it gains focus, but the NSCollectionViewItem which is behind the text field does not get selected.
I want to change this last behaviour so the text field gets focus and the view item also gets selected.
What's the best way to do this?
I believe you would have to subclass NSTextField and override mouseDown to pass the event to the nextResponder (which should be set to your collection view)
I also have an NSCollectionViewItem that contains an NSTextField. I'm trying to recreate the basic look and feel of the text label of an Icon in the Finder. I haven't finished it yet, but what I have so far seems to answer this question.
The key is to have the text field start out as NOT selectable. A mouse click on the text field will be ignored by the text field and will select the collection view item. Then in the collection view item's setSelection method, when it is being selected, set the text field as editable. The next mouse click on the text field will give it focus. Then when the collection view item is being unselected, set the text field back to not selectable and wait for the collection view item to be selected again.
-(void)awakeFromNib
{
[self.textField setSelectable:NO];
}
-(void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if (self.selected)
{
[self.textField setEditable:YES];
}
else
{
[self.textField setSelectable:NO];
}
}
This answers the above question, but obviously, more is needed for a complete solution.