Setting tag on Dynamic UITextfield - iphone

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.

Related

is it possible to change the style of uitableview in two different views? [duplicate]

This question already has answers here:
Can you programmatically change the style of a UITableView after it's been created? [duplicate]
(3 answers)
Closed 9 years ago.
i have a table and i have also declared its style as grouped table style in view did load but i also have a button on click of which i want to change the style of the same table.
initially i was setting this in view did load method:
tableObj= [[UITableView alloc]initWithFrame:CGRectMake(5,50,310,300)style:UITableViewStyleGrouped];
but on button click event i am setting tableview fram and style see below code
tableObj.frame=CGRectMake(5,50,310,300);
tableObj style=UITableViewStylePlain;
but it gives an error.....assignment to readonly property ??
According to the docs, UITableView's style property is declared thus:
#property(nonatomic, readonly) UITableViewStyle style
That readonly keyword means that you can get the value of the property, but you can't set it. You can only set the style when you create the table using -initWithFrame:style:. This agrees with the error message you received:
assignment to readonly property
Put simply, you can't do that.
tblView.style;
It is a read-only property you cannot set any value to while you can only read it which is set.. You can check whether the property is changable or, not by writing your code as below
[tblView setStyle:];
But you will find that you can't do that, so you can't set.
Better you get 2 tableviews, or, reinitialize your existing tableview with different style.
It possible to change the frame of table view dynamically but you can not change its style
You set the table style when you initialize the table view (see
initWithFrame:style:). You cannot modify the style thereafter.
UITableView Class Reference
You cannot change the table view style like that. My advice use two tables or do something like this.
Initially it is like this:
tableObj= [[UITableView alloc]initWithFrame:CGRectMake(5,50,310,300)style:UITableViewStyleGrouped];
Then when you want to change do this:
[tableObj removeFromSuperView];
tableObj=nil;]
//if not using ARC
[tableObj release];
tableObj= [[UITableView alloc]initWithFrame:CGRectMake(5,50,310,300)style:UITableViewStylePlain];
[self.view addSubview:tableObj];
According to the UITableView Class Reference, you can not change the style of a UITableView
When you create a UITableView instance you must specify a table style,
and this style cannot be changed.
See this.
Can you programmatically change the style of a UITableView after it's been created?
No this is not possible See this is wriiten in apple documentation:-

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.

How to access Object ID Identity attribute?

In the IB under Identity tab you can find an attribute called "Object ID". I can not find a way to get hold of this ID from code. Oh, and I know about the tag attribute but it's not what I need.
I essentially would like to get the unique object ID for a UIComponent that was touched on the sceen. I already have the UITouch object.
The Object ID in Interface Builder is only an internal book-keeping value used by IB when deserializing/serializing XIB files, and does no longer exist when the Application runs.
You want to use tag, or alternately, a property/outlet.
For UIView I normally use the tag property.
- (IBAction) buttonPressedid) sender {
NSLog(#"tag: %i", [sender tag]);
}
I'm pretty sure you can set the tag property in IB :)
use tags instead of the IB Object ID. As far as I know this object ID is only used in interface builder.
You can set the tag in the Attributes tab.

How to get current value of UITextField in cocos2d 0.8.2?

Hi all I am using cocos2d 0.8.2 for my new game. I have added a UITextField in my game and on click of a button (this button is actually a sprite i m using ccTouchesEnded! ) i save text field's value in database, but whenever i access the value i am getting nil. if i initialize the textfield value by some string, it always give me only that value.
i.e.
[txtField setText:#"Enter Your Name"];
when i access the value of txtField on a click of button it always give me "Enter Your Name". Although the value is changing when i type in textField, but its not returning me the new value.
is this a issue with cocos2d 0.8.2 or i am missing something?
You don't save the entered text. You just set #"Enter your Name" as default text, that is displayed in the cell. You should have a look at the UITextFieldDelegate methods. You have to overwrite the method didEndEditing, that is called after your editing or typing in text has ended, and implement there a line that saves the current value. Here you can use something like this:
NSString *containerForTextFieldValue = textField.text;

Assigning different labels dynamically

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!