Iphone, user created labels? - iphone

Usually When i want a object to be accesed by many different event calls i just declare it in the h file.
However im making a app where i need to allow the user to create UILabels and drag and drop them where they want. So i have the code to make the labels and a alertview where they change the text.
-(IBAction)AddText{
UITextField *TextView;
TextView = [[UITextView alloc] initWithFrame:CGRectMake(40.0, 40.0, 40, 40)];
[self.view addSubview:TextView];
Which works however i need the user to be able to create many textviews which each have a unique name and a way i can reference them later such as TextView.text, doing it this way always creates a object named TextView and i can only reference it from his event, otherwise its undeclared.
Im really stuck at how i would go about doing this if anyone could help it would be great!,
Thanks

You could think about adding the newly created label to a NSMutableArray or, better, to a NSMutableDictionary. This latter solution gives you the possibility of creating a dictionary in which each label can be referred to using a string key. If you put such a dictionary in your .h file you'll be fine, I think.

How about creating an NSMutableArray of NSDictionary items. The dictionary items could have a reference to the label as well as things like name, and other relevant meta information.

You can add the UITextViews to a collection of some sort, depending on how you want to retrieve them later.

Related

creating an iPhone app without using the Interface Builder

I'm looking into creating a simple iPhone app without using the interface builder, i.e. without creating the XIB files.
I've succeed so far in showing the main window and changing the background color, but i'm looking into adding UITextfield, UILabel, and a button. and then connect them to methods that i've created before.
is there any good tutorial or a reference that I can use?
Thank you very much
Read Apple's documentation. For example, to create a UILabel programmatically, read the UILabel Class Reference. First instantiate it
UILabel *label = [[UILabel alloc] init];
Then set it up as you like, e.g.
[label setText:#"My label now has text!"];
Then add it to the view
[self.view addSubview:label];
Then move it around, etc. For more help, first google "create uilabel programmatically", then, if you get stuck, post a specific question here.

Different methods to display NSarray

I have an array which store highscore records, besides using tableview, is there another way i can display my array.
That's the best way, if you're using an NSArray as a datasource; if you don't need the users to interact with the data, you could use a UIScrollView, maybe, by adding a UILabel for every data row, though this seems clunky.
Your other option, and probably the best option, is to customize the look of the tableview, as shown in this tutorial.

Passing object data with UIButton press

I've created a custom UIView class FormDropdown, which contains a question & button in the nib. In the class is also an NSArray property which is supposed to store the various options for the button.
So a button can be placed by doing this, in for instance a viewDidLoad method:
FormDropdown *dropdown = [FormDropdown dropdownWithQuestion:#"This is an example question" andLabel:#"Select one" andOptions:[NSArray arrayWithObjects:#"One", #"Two", #"Three", nil]];
[self.view addSubview:dropdown];
Obviously, I'd like the button to, when tapped, bring up a UIPickerView with the options showing. But I'm stuck on how to send the options to any method. I know I can attach an action to the button like so:
[dropdown.dropdownButton addTarget:self action:#selector(dropdownPressed:) forControlEvents:UIControlEventTouchUpInside];
..but I can't see how I would pass the options from the dropdown.options array to the method?
I believe that you can do this by adding an "associative reference" from the UIButton to your object data.
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html
I am looking for a way to do that as well... however, it doesn't seem possible.
My possible solution: I think I am going to create a subclass of UIButton, and add a "NSObject *tagObject" property to it.
Anyone seems something wrong about it? (I am using ARC, and I am wondering if that would cause objects to remain in memory - I do not think so).

How can I programmatically access UI elements in a NIB without 'wiring' them?

I'm contemplating writing some helper functions to make it easier to do simple changes to the UI elements in my iPhone NIB. Mainly - I want to access a UILabel, or other element, via its Name in Interface Builder. Is this possible? Is there a smarter approach?
Example
Say I want to make a super simple iPhone app that displays 'Hello World'. I start a new project and then open the NIB and drag a UILabel into my view and give it a 'Name' of 'LblMain'. Now, presuming that I've included my handy helper function, I'd like to assign the label some new text something like this:
[helper setText:#"Hello World" forLabel:#"LblMain"];
-or-
UILabel *ObjTmp = [helper getUILabel:#"LblMain"];
ObjTemp.text = #"Hello World";
Now - the trick is that I didn't add a:
IBoutlet UILabel *ObjLblMain;
anywhere in .h file - I'm just accessing that label dynamically - wouldn't that be nice?!
Now, for simple apps, to add some more labels or images, I could drag them into my NIB, assign them names in that element's inspector window, and then immediately access them inside code without the stuttering & hassle of adding them in the .h file.
Motivation
Basically, I'm frustrated that I have to wire every element in my NIB - it's a lot of stuttering and bookkeeping that I'd rather avoid.
I could give a design some naming conventions, and they could generate a NIB without needing to be intimate with the implementation.
Name is 100% not accessible after the object is loaded, something I always thought was odd too.
What is accessible is "tag", if you really want to access an element without defining an outlet you can set the (integer only) "tag" value, and then within the superview that holds the tagged element call viewWithTag: passing in the same integer. Don't forget the default is "0" so use something else.
You can definitely load the NIB programmatically, find all the objects and query them to work out what points to what. Just look at Loading Nib Files Programmatically. But the problem is that the Interface Builder Identity Name isn't exposed outside of IB. So I'm not sure what you would use as the "forLabel" parameter. The "Name" field is just a convenience for the IB document window. It's not a property on NSObject.
It can be done by the element tag:
Lets say you have UIView xib file called "yourView" and inside it there is UILabel that you want to access it without wiring.
Set a tag to the UILabel in "yourView" xib file, lets assume you set UILabel tag to 100.
After loading "yourView" anywhere you can get UILabel without having any wiring by using this code.
UILabel* yourlabel =(UILabel*) [yourView viewWithTag: 100];
//do whatever you want to your label.
I think you can try opening the xib in some external editor as XML and see how the compiler sees it, then you might possibly do the same way
For iOS6+ you can use restorationId instead of tag, to make it more "readable", for example you can set the same name in your nib file and in restoration id.
If you do not want to link all the outlets from your nib to your viewcontroller, you still can access them by searching in your current view subviews tree. Note that subviews arrangement is a tree (the same tree that you can see in your nib file), so you will need to do some recursion if you have nested views.
For example:
UIButton *nibButtonView = nil;
for (UIView *view in [self.view subviews]){
if ([view.restorationIdentifier isEqualToString:#"myNibButtonView"]){
nibButtonView = (UIButton *)view;
}
}
[nibButtonView setTitle:#"Yeah" forState:UIControlStateNormal];
In your nib file you should have a button with a restorationId equals to "myNibButtonView", you can find the restorationId textfield in your identity inspector (third column of utilities)
You may use this if you have a huge number of outlets a you don't want to linked them all.

Save UITextView content in NSString

I'm trying to save the contents of an UITextView into a NSString which I will somehow persist later.
The problem is that the UITextView "text" property doesn't save all the rows in the textView (after touching "return"). I tried print the TextView object and the textView.text, and they're different.
Does anybody know how (after editing the textView) I can save its content into a String (or something else that I can later access and share through different views and persist in the database)?
Thanks a lot. Pretty sure it is simple, but I'm honestly not finding the solution.
NSString *textViewString = myTextView.text
ought to be what you need. But you say that the TextView object and its text property have different values? Has your delegate received – textViewDidEndEditing: or – textViewDidChange: yet?