Assigning different labels dynamically - iphone

After detecting Button sender through its tag in an action method, I want to dynamically assign some text to the corresponding Label (say, button tag+10 ) using some sort of temp UILabel var.
Hoping that's understandable?..english is not my native language :)
thank you.

In the containing view, you can use viewWithTag to get the object with a specific tag.
If you know the object is a UILabel, you can typecast it to get your label object.
If you are in your viewcontroller.m file:
UILabel* myLabel;
myLabel = (UILabel*)[self.view viewWithTag:(buttonTag+10)];
Good luck!

Related

Access UITextField with tag on a static Table

So I have a static TableView setup in the interface builder with multiple sections, labels, and text fields.
I'd like to be able to something along the lines of
[self.view viewWithTag:kNameTextfield]
and get the UITextField with that tag that I've set.
For some reason I cannot access the UITextField and it comes up Nil. I imagine because it's somewhat deeply nested? I thought about creating an Outlet collection of all the text fields and other than iterating through them every single time I need to change something seems like a waste?
What I'm trying to do is on initial load, it populates the values of the textfield by values in a dictionary. So it's useful to be able to target a specific textfield.
Try iterating through the subviews:
for (UIView* viewsLevel1 in [cell.contentView subviews])
{
for (UIView* viewsLevel2 in viewsLevel1)
{
UITextField *textField = [viewsLevel2 viewWithTag:kNameTextField];
}
}
Go as far deep as you need to go

Setting tag on Dynamic UITextfield

If I create a UITextField programmatically, will it have a value for the tag? Can I set a value for the tag? Basically I have an app that allows someone to hit a plus sign and a new uitextfield is created. I need to capture the value of the textfield and track which field that value was for. I was using the tag property to determine which uitextfield was which and then storing it according. Basically, I wanted to assign a unique id to each uitextfield. Is it possible to set the tag? I know I can extend the UITextField class and achieve it that way. Is that the only way?
tag is a property of UIView. A UITextField is a type of UIView. Therefore, you already have a tag property available in your UITextField.
Its default value should be 0 but it's certainly writable, so something like textField.tag = 42; is valid.

XCode - Whats the best way to change the identifier of UILabels that are created in a loop

I am creating labels in a loop.
How can i make it so each label has a different identifier so i can retrieve the text of a specific one at a later time?
You need to add the label to a dictionary, and set the key to your labelName, and the object to your UILabel.
Then, whenever you want to access the variable, you use the objectForKey:myVar method of the NSDictionary
Objective-C is a static language so you can't. You should create an array or dictionary, depending on your needs, to put your labels in.
And by the way, don't forget asterisk when you are declaring pointers.
I do it by setting the tag of the label
if you do it through a for loop you could do :
for(int i = 0; i < max; i++)
[aLabel setTag:i];

Grab Text From Dynamically Created UITextField

I am trying to grab the text from a dynamically created text field. I use this to make the text field become and resign first responder:
[(UITextField *)[self.view viewWithTag:0] becomeFirstResponder];
That works fine, but when I try to get the text, the app crashes.
[(UITextField *)[self.view viewWithTag:0] text];
What am I doing wrong?
Do not use the tag 0. viewWithTag: searches the view hierarchy starting from itself and since all views start with tag 0, it will identify itself as the view to be returned.
I suggest that you use a different tag on the text field.
Since the default tag for every UIView is 0, I'm going to guess that there are multiple UIViews with the same tag (tags aren't guaranteed to be unique). Instead, choose an arbitrary high value like 1000, then increment that with each view added.
Also, it would help if you included your UITextField creation code.

1 tag multiple UILabels in xcode, iphone

So I have many labels in my app that contain the same text. I am localizing them, and would like to know if its possible for me to tag them all with the same number and assign the new text that way.
currently when I have tested it only recognizes the first label with the tag.
I really want to save myself from having all these repeat outlets
Yes,
You can tag them with a integer number because in inheritance tree NSObject is the super class to UIView,
And NSObject has an integer property called tag .
USE tag like below
myLabel1.tag = 1;
myLabel2.tag = 2;
...........
...........
myLabeln.tag = n;
It is a little inclear from your question what your problem is, but I am assuming from your question that you want to be able to update all your UILabels which are located at different places within your app with the same text.
You could create a global header file that contains an NSString which holds the text for all your labels, and then #include this header in all view controllers which contain labels which need to show this this text. Then whenever you update the NSString in the global header you update any labels that are currently in view or about to be viewed aswell. This can be done from within your IBAction methods, as well as implimenting the viewWillAppear:(BOOL)animated method. Whenever a view that contains relevent labels comes into view, it will apply the value of the NSString in your global header and update its labels.