UIView viewWithTag 0 problem - iphone

On view in IB I have numerous items (including view itself). ONLY one item has tag 0, yet following line works for ANY UITextBox other then the one with tag 0. Note that only ONE UITextBox has tag 0, why:
(UITextField *) [self.view viewWithTag:0]).text = #"foo";
Interesting that when triggered event received for the element with tag 0, I can get value of Tag 0 with no problem.
Is there a limitation to look for tag 0 elements?

All views have a 0 tag as a default so if you get a 0 view it could be any view. For it to work you need to use non-zero values that you set in your program or within Interface builder.

actually, if you ask a view called "X" for a viewwithtag 'zero', you will probably get the "X" view as the return value :/ idiotic behaviour. send regards to apple

que is right. I just got bit by this.
From the docs:
Discussion This method searches the current view and all of its
subviews for the specified view.
At least it's documented correctly. I guess we need a method called "subviewWithTag:"

set self tag as -1. won't affect other view's tag.

Related

Subclassed UIView doesn't get tag set

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.

UILabel.center is 0? how is that possible?

I'm trying to get the CGPoint from the center property of some UILabels (which is part of an outlet collection). Now, when i do:
for (UILabel *label in labelCollection) {
NSLog(#"%f", label.center.x);
}
It simply returns 0, same with the center.y? how come? i need those coordinates hehe :)
thanks on advance
//EDIT
Uploaded a picture: the labels with "a" "b" "c" "d" is the labels i'm talking about. i tried changing the text.property in the code and that works well, but it just wont give me the coordinates it's on.
Verify that the view has a superview. The value depends on the superview's coordinates.
I figured out the answer, it had to do with the fact that i was doing the whole thing in viewDidLoad, and i should be doing it in viewDidAppear. it must be because on viewDidLoad, it hasn't been placed yet, and therefore i could still change the text, coz the object label was created, but i couldnt get the origin coz it was not placed yet.
thanks for helping lads.
You remove the constraints then run the app may be constraint create the problem because new xcode constraints create problem on some functions and properties so first remove the constraints by uncheck the "Use autlayout "then you check the app

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.

Adding Subviews programmatically vs. adding them in InterfaceBuilder

I have a beginner's question.
I was wondering about the level of subviews and how to determine them. Consider having put one button in IB (sampleButton) and then creating a subview programatically, like so:
[view insertSubview:aView atIndex:[view.subviews count]];
which is simply equivalent to:
[view addSubview:aView];
Now, in both cases, my index (which is referred to as z, right?) should be 1, if I am not mistaken.
I have one button added to my view in IB which should be at index 0 (z = 0, or do they start at one?). Then, if I do a view.subviews count, this will give me the number 1, as I have 1 object at index 0. So my new subview 'aView' will be placed at index = 1 which is just one layer above my sampleButton.
So far so good. But what if I did not want things to be this way? What if I wanted to be the sampleButton at a higher level (e.g. 1) and my 'aView' at the lowest (e.g. 0)? Can I simply do this:
[view insertSubview:aView atIndex:0];
Will this simply take care of my button created in IB, pushing it to another layer or will I have to tell IB that sampleButton shouldn't be at z=0?
It behaves as you already guessed: if you insert a subview at index 0 it is "below" all the other subviews.

Using tags in iphone programming--can someone explain?

I've seen people using tags in iphone programming, like inside labels or tableview cells:
name.tag = kNameTag
Can someone explain with an example how these tags might be used? I gather that it's so that you can refer to a ui element later? Like if you programmatically use a for loop to create an array of UIButtons onto the iphone screen, do you assign tags to each button within the for loop or something?
Thanks!
The example you've included in your question is one of the common ones.
You can instantiate buttons (or other UI elements) in a loop, assigning a incremental tag to each. When an IBAction is invoked by one of those buttons, you can ask the sender for it's tag, which tells you exactly which button triggered the request.
for( int i = 0; i < 10; i++ ) {
UIButton * button = [[UIButton alloc] init...];
button.tag = i;
}
IBAction:
- (IBAction)doSomethingFromButtonTap:(id)sender {
NSLog(#"Button pressed: %d", [sender tag]);
}
They're also widely used to find specific subviews within a parent view. UIView provides a viewWithTag:(NSInteger)tag method. This is useful when building custom views without subclassing (or situations where you don't want to hold references to subviews, but know the tag).
Tags are integers. You assign them to a view using UIView.tag. You then use -[UIView viewWithTag:] to search the view hierarchy for the view.
UIKit doesn't use tags (I think), so they're free for you to use as necessary. However, tags are global to your app, so they're not an ideal replacement for IBOutlet (but it's often more convenient when you have a lot of views).
Avoid using 0 as a tag, since it's the default tag — [v viewWithTag:0] is unlikely to return the view you're looking for.