How to delete the contents of a UITextField programmatically? - ios

I am writing an iPhone app that has a number of UITextFields that requires the user to input a bunch of data. I would like to have a simple 'delete' facility that allows the user to discard any data they have put in to the multiple fields.
I've been struggling with this issue for a few days now. I can't seem to find a way of reaching into the individual text fields and changing the text. My app throws errors at me no matter what I try.

You can just assign tags to each textfield in a sequence using tag property in interface builder and then get the textField like this
UITextField *txtf = (UITextField*)[self.view viewWithTag:aTagValue];
txtf.text = #""; or nil;

There are several ways of tackling this.
You can write code to wipe each of them individually. To do this, you create an IBOutlet for each one, and assign a blank string to their text properties. If you only have a couple of fields, that's simplest. If you have more, it's not so good.
You can make them all children of a common container view and loop over its subviews. That way, you only need to hook up a single IBOutlet.
You can recurse over your entire view hierarchy and blank out all the text fields you find.
Another approach, which isn't very well documented by Apple, is to use an IBOutletCollection. That way, you can assign all of your text fields to the one IBOutletCollection and loop over it. This is probably the simplest, most straightforward way of doing it.

Related

iOS Save Text from Dynamic TableViewCell

I am building a part of my app that uses a Dynamic TableView with a UITableViewCell class with 1 label and two UITextFields. One textfield is for the user to type a question and the other is for them to type an answer. The goal is to save the text from the two text fields into CoreData. I am however unsure how to correctly identify one textfield from another since there may be 4 or 5 rows with the same text fields in the table.
I've attached a screen shot of what it looks like.
Should I be using tags on each textfield? If so, how can I set that up?
Thanks for the help!
I'm not sure your exact setup, but it seems easiest to use tags.
When you set the placeholder text for each "He said/I said," also add a line textField.tag = <uniqueTag> where you (arbitrarily) come up with a unique tag for each UITextField that you have. Then, when the user hits save, or when you get a call to a delegate method for all of these text fields, you can differentiate them by checking their tag property.

Placing UIView or UILabel inside UITextField or UITextView acting like character

I need to create a dialog, similar to the one that is used to write functions in Numbers from APple, for example.
User can select range in the table, which is then represented as any kind of UIView inside text input line. When long-tapped, the content can be edited like normal text, but what is most important is that whole subView, containing text with range description is treated as one character.
Does anybody know or have idea how to achieve this?
I would appreciate any hints...
I had to deal with something similar, to have custom UIView(s) in a search field.
I'm not aware of a "simple" way (ie, API) to achieve your goal. You need to create a custom component that looks like a classic field, but isn't one.
To be a little more specific:
Subclass an UIView (or directly an UIScrollView)
Have inside a TextField (or UITextView depending if it needs more than one line) to handle the "keyboard".
Put some custom UIView/UILabel before the textField/textView and adjust the textField/textView origin depending these views/labels
If you want to have a label/view IN the text, then it's a little more complex and you'll need something even more custom:
Handling the keyboard events without a textField/textView (there is a way but I forgot how exactly)
Drawing the text and adding views/labels in specific locations.
Of course all depends on the component you want to create ^^ there is no one simple answer.

Creating NSArray of UITextField objects

I want to have a large number of text boxes which will be touch enabled and editable. Is creating NSArray of UItextField objects the best way for this? If Yes, How can I create? or Suggest other ways to achieve this.
It largely depends on what you are trying to do. An NSArray as a way to store all the text boxes you are using in you controller (instead of creating ivars for that purpose) is ok, but you could as well use a UITableView/UITableViewController for that.
Using a table view would give allow you to grow the number of your text boxes without any effort. On the other hand, if you can guarantee that your text boxes will never be more than those you can display on a single screen real estate, I don't think using a table view would give you big advantages. But, as I said, this largely depends on what you are trying to do.
If you decide to go for the array option, I would suggest using an NSDictionary instead, so that you can access each one of your views by name (or tag, if you associate a tag with each one).
Also keep in mind that you could use the getViewByTag: method on your container view to get a reference to any view that it contains based on the view tag you assigned. So, you could do:
//-- creating text box:
UITextField* textBox = ....;
textBox.tag = 1;
[self.view addSbview:textBox];
//-- accessing the text box:
UITextField* textBox = [self.view getViewByTag:1];
In this sense, a view already behaves as a container for you text boxes and gives you access to them.
EDIT:
Actually I'm trying to create a crossword grid
ok, so, if it's 2-dimensional, I would say that a table view is ruled out (it is not impossible to do, but I think there are easier ways).
as to your question, it all depends on how dynamic your crossword grid is: does it always have the same number of rows and columns? or can it be defined by the user? etc.
In the first case, I would go for an NSArray, or I would simply use tagging as shown above (that would also make memory management automatic).
Otherwise, you might inspect UICollectionView.
If your question is: which data structure is more appropriate to handle a crossword puzzle? then, have a look at this post. In any case, I would say: do not expect that you find a ready-made solution for that kind of problems...
A UITableView containing editable cells would be the best way to do this, if you're after what I think you're describing. There's lots of sample code on Apple's developer site detailing how best to use a table view to create a view showing a series of editable text inputs.
Better to use UI Table View instead of adding 'n' number of text fields.

iphone/ipad how to handle lots of input fields?

I'm writing a app that contains quite a bit of input fields for collecting data.
and im wondering what are some good methods to display these kind of input fields, if there are too many to fit on a screen? so something like the add contact screen... where u can scroll down and there are fields there
my initial idea is to put them in a scroll view and then i can scroll through them, is there a tutorial to do this? it seems like the scroll view is more for dynamically displaying data like a text view, and not for static forms like i was describing.
if anyone has any different methods for doing this please post.
UITableview will match perfectly for what you need.
My friend wrote this which is a container view that automatically helps with moving fields out of the way of the keyboard - It will certainly save you some time if you don't want to use a UITableView:
https://github.com/mackross/GTKeyboardHelper
The other way as H2CO3 suggested is to use a UITableView. If it is a UITableViewController, then you get the moving out of the keyboards way automatically. You could build custom table view cells that are styled to have a prompt and a UITextField for input. You can also set the selectionStyle to UITableViewCellSelectionStyleNone to prevent these cells from highlighting when selected.
I think the third way is to do this with a UINavigationController (or a UIPageControl) and build a kind of wizard, where you go through various pages of related data. This might be the neatest way depending on how many fields you have and if you can group data into common sets (e.g. personal information, work information etc)
I had the same problem and found GTKeyboardHelper to be an easy way out.
After drag and drop the framework in your project, include the header file.
Download and open the example project, then drag the "Keyboard Helper" object from the objects section in the xib to the objects section in your project's interface builder.
Drag and drop all your views to be children of the "Keyboard Helper".

iOS - Adding Labels On Demand?

I am working on something similar to a canvas, users can add drag labels onto a view, resize them and whatnot.
My Question for you is: What do you think is the best way to load these labels programatically?
This may sound foolish at first but I do not know how many labels there will be, so I do not want to hardcode n Labels and find the user uses n-200, if you see what I mean? I am aware that it is simplistic to do:
UILabel *myLabel = [[UILabel alloc] init];
but then I have one label called myLabel, is there any way to programatically name these variables? As we all know we cannot have more than variable with the same name.
An example may help:
User drags on 2 labels, saves this view. I come to load it later, programatically. What is the best way to create and name these 2 labels, having no prior knowledge of how many labels there were (i.e. cannot be hardcoded).
I am aware this is a shoddy explanation but, if someone can wade through my crap and see the question, please add your 2 cents!
You could assign a numeric value to the "tag" property of the UILabel if you're trying to somehow have an identifier associated with them. (If I'm understanding your question correctly)
Or if the question is how to load them, you could use NSMutableArray and then just keep looping through your saved labels, create them, assign the new UILabel reference to a new entry in the array.
Again, not 100% sure of what the issue is that you're facing though maybe this can help.

Resources