iOS - Adding Labels On Demand? - ios

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.

Related

Using a PList to integrate with buttons, not a table view

Right now I have a quiz that I made that has five seperate categories. There are certain stipulations that I need to keep in order for it to be used in its intended use. I started out with a simple switch statement to run the program because it made sense to me at the time and I thought I would be able to manipulate the quiz to run the way I want. Well, long story short I ran into one issue. My quiz runs like this:
Intro screen> Category selection screen where the user chooses one of the five categories > Question screen. But, the way that it runs after a user answers a question, it displays the correct answer and whether they were correct or incorrect and displays a button to go back to the selection screen. I could not figure out how to run through an entire category of questions and still maintain that correct/incorrect screen that displays each answer between each and every question. This is a learning tool, not a strict test and if they can't see the correct answer it is pointless.
So I started reading up on PLists. Trying to make this a more dynamic code has been confusing, but the only way I have figured out how to use PLists is using a table view, which changes up my GUI. Is there a way to still integrate a PList but maintain my same buttons and UILabel that will change with each question?
I.E here is an example how how it is now:
UILabel (This label hides until after a question is selected where it changes to Correct/Incorrect)
UILabel (changes with each different question)
Button1 (These four buttons changes with each respective answer)
UILabel (This label stays hidden until after a question is answered then will appear with the correct one)
Button2
Button3
Button4
All four buttons are set to hide after one is selected.
Is there a way pull data from the plist instead of using a table view to make the quiz. The spacing and the background image depends on using this type of interface over the tableview.
A side note question would be, would using Core Data be able to use this approach? Not a super important question because I think that Core Data from what it looks like is much too advanced for me. I do have a 2500 question quiz I will be making though pretty shortly and was thinking about looking it this.
Thanks for the help again guys! You have been very helpful. Is there way to get in contact with developers for more private help? I am currently deployed right now and it has made it very difficult finding any help while I am learning. So far I have read through Programming Objective C 6th edition and am about to start on the iOS development book I have and it would be nice to have someone to shoot a simple question to for a little clarity.
You probably want to create a singleton question controller (or at least instantiated a single instance with the selected category). Your other view controllers should probably be recreated each time you move to a new question / answer. The question controller manages knowledge of the selected category and which questions have been answered. It can also manage the score, question randomisation, etc.
The plist makes little difference really, only the question controller should know of it's existence (also the category controller perhaps).
2500 questions depends on the amount of text. Try it and see. The problem with a plist is that it is all loaded in memory at the same time. You could use a plist for the structure and other files for the text content of each question / answer.

Do I need to create a Storyboard for every Q&A in a quiz/trivia type of iOS applicaiton

Please forgive me for my n00bishness.
I am creating an app that asks several questions, and scores the result. Similar to something like an IQ test.
I would like to not have to create a storyboard for every question. I was wondering if I could create a storyboard for each question "type" and then populate that storyboard with questions from an array? For instance, while I may have 100 questions in the app, I want to only give the user 20 each time they run the app and I really only have three question "types":
Multiple Choice (one correct answer)
Multiple Selection (multiple selections allowed)
Yes or No
I would like to be able to create a standard screen for each of these types of questions and then populate the screen with text and scoring parameters from some sort of array.
If that is possible, I would probably like to create a large question set and then only feed in say 20 of 100 possible questions in a random fashion.
It seems like I also should create a "Question" class, and then spawn instances of that class for each question.
Is the the correct way to proceed?
You only need to create only one view controller for all the questions you are going to display, maintain an array of questions and make IBOutlets and IBActions for all the display components, Maintain current index of the question and write the logic for updating the question.
You can create one view controller in storyboard and then you can use different View for each category. Whenever any category is selected you can Hide/Show those corresponding views.
What you are thinking is the right approach.
You need to create just one storyboard which you'll be using to display the questions. The questions and the options being displayed on it would be dynamic.
Comment in case you are not able to find a tutorial for the same.

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.

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.

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