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.
Related
I have subclassed a UIView class and I create multiple instances of this class in a loop (incrementing each time), however when I try to set the tag of the view, and log it to the console after they are all created, they all have a tag of 1, regardless of what I set the tag as.
Any help would be appreciated, Thanks!
My Code for creating the subview is here:
//for() loop above with i as counter
FlashCardView *subview = [[FlashCardView alloc] initWithFrame:frame];
subview.delegate = self;
subview.viewNum=i+10; //My attempt at a workaround but I cannot get the view with this later so it is not very helpful
[subview setTag:i+10]; //Tried this and subview.tag=i+10;
NSLog(#"%d", subview.tag); //Prints correctly
//Gets added to parent later
This NSLog logs the correct tag, however when I log the tag in the UIView subclass, it always returns its tag as 1. Also, if I print all the subviews of the parent in a later called method (In the viewcontroller), all of them have the tag 1.
I cannot tell you why, but I can tell you how to find the problem. In your FlashCardView subclass, add this method:
- (void)setTag:(NSInteger)theTag
{
assert(theTag != 1);
[super setTag:theTag];
}
Then, when whatever is setting the tag to 1 does it, the assert will fire and you can look at the stack trace and see where its coming from.
Alternately, remove the assert, and put a breakpoint on the super message.
PS: make sure you enable exceptions!
Hi thanks for all your help, I found that I had accidentally typed in the subclass's variable when I meant to put in something else, setting the tag to 1. #Sunny, thanks for telling me to double-check.
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.
I have a piano keyboard. Each button (key of the piano) has a different tag assigned in IB. The tag is used to identify each button so I can play the sound that belongs to that piano key. I connect each piano key to the following IBAction:
-(IBAction)playNoteFromKeyTouch:(id) sender{
UIButton *pressedButton = sender;
int tag = [pressedButton tag];
}
The value returned by [pressedButton tag] is always 1. I have tried different ways to get this, for example:
-(IBAction)playNoteFromKeyTouch:(id) sender{
NSInteger tag = ((UIView*)sender).tag;
}
And simply:
-(IBAction)playNoteFromKeyTouch:(id) sender{
int tag = [sender tag];
}
No matter what I do, even when I assign tags programatically to each piano key, I always get tag == 1. What could I be missing?
I think, you should go with another approach. Create an Octave class, subclass your keys from UIView, add an octave property and an note property to the key.
alternatively you can have an array for every octave and check what index in what array the sender has to determine the note.
tags are nice, to identify a view loaded from a nib-file. for anything else proper object-orientated object handling is better!
I put together a test project and verified that your code should be giving you the appropriate tag. Perhaps you overlooked something outside of the code. Try the following:
Verify you changed the proper .xib (iPhone instead of iPad, etc.)
Verify the tags are properly set in the .xib
Restart Xcode and verify that the tags are still set
hi i am new to iphone development. what i did is i am creating a class named imagepicker.h along with imagepicker.m,for that classes i am creating imagepickerviewcontroller.h and imagepickerviewcontroller.m.... in the imagepicker.m i am declring a button for that button assing a image tag value. "now what i need is how to get that buttion.tag value to imagepickerviewcontroller.m" please help me thanku
Declare the button in your imagepicker.h, make it a property and access it in you view controller by creating an object ob imagepicker class. imagepickerobject.button.tag
When I use the this method to initialize a UITabBarItem:
- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
Do I need to have a distinct tag for each tab bar item, or (since I don't use them) can I simply use the same tag value for all of them?
I'm pretty sure you can just leave them all as 0 or any other number you choose. Every UIView can potentially have a different tag, and Interface Builder sets them all to 0 by default. I haven't run into any problems with this.
From Apple's UITabBarItem class reference:
tag - The receiver’s tag, an integer that you can use to identify bar item objects in your application.
So it looks like it doesn't really matter.