Creating NSArray of UITextField objects - ios

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.

Related

Adding a UIView based on a template in swift?

I've used UITableView before and like the way that the user can add a practically unlimited number of cells by entering information and the program uses a template. I'm wondering if there is a way to do this, but instead of using a table, using regular views or even buttons. For example, the user would tap a button, enter information, and return to the first ViewController and it would have a new view with the information in place of parts of a template that I designed.
Sorry if this is unclear. Basically I'm wondering if there is a way to make a table that is not as restrictive as a table, but uses several individual views in place of cells.
You can create re-usable views in the same way that you can create prototype cells.
Just right click in your project window, add new file and select User Interface > View. create it just like you would a prototype cell.
Then create a related class by adding a new swift file, link the two, create any outlets or actions you need and add any required logic.
Once you are done you can just load it wherever you need it, like so
self.headerView = NSBundle.mainBundle().loadNibNamed("HeaderView",owner:self,options:nil) as! HeaderView
You would likely need to pass in some required information, or setup the views frame or constraints.
I found a YouTube Video which should help guide you through the process. I've only skimmed through it so you may need to look around for a better one, but the general concept seems to be there.

How to work with dynamic content

I am working with a UITable View and I am trying to figure out the best way to handle the way I display the content as its dynamic.
For instance some of the things I search may have a title and a description but then other times only have a title.
In a n instance like this I would like to be able to control the positioning of the labels in my UITableCell, of which I have created a custom one.
For instance this is a graphical view of what I am trying to do.
Title:
No Title:
As you can see with the second option I have decided to move the UILabel to the left where the Title label would be.
So What would be the best way of doing this?
I am thinking maybe creating several different format types in a .xib and then just using if statments.. if its missing such and such then use cell a or b or c.
Or is there a better way to achieve this?
Any help or advice would be greatly appreciated.
The different formats in .xib files will do the trick, just instance the proper cell type according to your needs, the other is delving into how you draw your cells, adding labels as you see fit when you get your data and drawing them appropriately.

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.

How to delete the contents of a UITextField programmatically?

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.

Resources