I have question regarding about finding subview using '.tags' in one UIView.
for (UIView *viewObj in [self.view subviews])
{
// want to find viewObject by its tag.
// but I heard for-switch or for-case paradigm are bad pattern design by WTF.
// there are total 9 of tags will be use for finding view object
switch (viewObj.tag)
{
case 0:
.
..
...
}
}
How can I make this to be non-switch or non-if statement? Should I just add into one array then fetch from there? For example, you add views with desired tags and fetch from this array.
Thank you.
From your comments, it's a very finite set of items that will exist - 9 items. If that is expected to be static, there is nothing really wrong with a switch statement. Perhaps you can functionally decompose each switch into a separate method. That said, perhaps the command pattern would be one approach to consider, in addition to your array idea. For reference: http://en.wikipedia.org/wiki/Command_pattern
If it is only going to be a fixed number of views and each view has different methods that need to be performed you could access the views directly.
UIView* someView = [self.view viewWithTag:0];
//Operations on view 0
UIView* someOtherView = [self.view viewWithTag:1];
//Operations on view 1
//...
One thing you could do is have the UIViews in your subviews be of the same type by having each conform to the same protocol. Then you could cast each to id<YourProtocol> and call the same method within each UIView.
And to further Shan's answer, if you can decompose them into functions and put them (function pointers) into an array, then you may be able to make it a little simpler, or you can use the new "Blocks" language feature.
There's a new IBOutlet type, IBOutletCollection. You use that for an NSArray, and then in IB you can add views to that outlet - at runtime, they will all be put into the array (unordered).
To get out a specific one you want, in viewDidLoad you could map all of the NSArray entries into a dictionary keyed by tag value, then just use "objectForKey" to get them out.
Related
I want to find a way how I can compare the actual hierarchical order of two subviews.
As i didn't find a method who turns out to give me this kind of result, I want to ask you. Im sure there must be a way to do this.
Is it possible, that the [view subviews] array is ordered with the same hierarchy?
Thanks,
Makrus
Although I have not find any reference about that in docs I'm pretty sure that the order of elements in subviews array corresponds to their z-order. You can try to change order of subviews in IB and log subviews to console - you will see that output supports that.
One more hint that it is really so are insertSubview:atIndex: and exchangeSubviewAtIndex:withSubviewAtIndex: methods which change subview's z-order and those changes are reflected in subviews array order...
I want to populate a scrollView with quite a few different UI elements.
Therefore I thought I would write a method that remembers the current Position in the scrollView and just adds the element to the scrollView at the current Position.
Something like:
- (void)addUIElement:(id)element withWidth:(CGFloat)width andHeight:(CGFloat)height andYGap:(CGFloat)YGap {
element.frame = CGRectMake(currentScrollPos.x, (currentScrollPos.y + YGap), width, height);
[scrolly addSubview:element];
//And then set the current scroll position here
}
Unfortunately when I try to do access element.frame = ..., I get request for member in something not a structure or union. When I try to do [element frame] = ... Lvalue required as left operand of assignment.
Now, first of all I am not sure what's the best way to dynamically add objects to a scrollview. Maybe anyone has a better or easier approach.
Then on the other hand, I don't get why the above does not work?! Would I have to cast my element to the actual class? I thought I would not have to do so... Also then my method would not make that much sense anymore. Or at least would require some more steps...
This should work I think:
[element setFrame:...];
However if you work with different UI elements in your method may be you can make your elements parameter UIView* instead of id? This way your code will work for all UIView subclasses (which is what you actually need I suppose)
The difference is that "id" doesn't have any kind of reference to a frame. It could be anything. You want to instead do (UIView *)element in the method declaration, or alternatively in the call to element.frame, you would do ((UIView *)element).frame.
(And yeah, all things that you put on the screen are inheriting from UIView -- UIButton, UIImageView, etc.)
For instance I have created a view with 50 sequential buttons named btn1-btn50. If I want to address these buttons and make some changes to them in a loop how can I address the controls with a string name?
The typical way to get a view in a hierarchy is to call [parentView viewWithTag:] to get the view. If you give the buttons tag values from 1 to 50 you can use that to access the buttons.
If for some reason you need strings, you will have to create a custom subclass of UIButton that has a name member, assign a name to that member, then later iterate through the view hierarchy searching for an instance of your custom class with a name matching your search criteria.
If you have given them a tag, you can access them conveniently using -viewWithTag: on the parent view.
You have no guarantee that these buttons are laid out in memory sequentially, so trying to use pointer arithmetic is probably out. But you can get all the subviews of an NSView with -subviews, and just do something with the NSButtons:
for (NSView *view in [theView subviews]) {
if (![view isKindOfClass:[NSButton class]]) continue;
/// you got a button!
}
In your View Contoller add an NSMutableDictionary *buttonViews property. In your viewDidLoad method, add each button to buttonViews using the name string as the key and the button as the object. You will have to use viewWithTag: already discussed to obtain the views. Now you can locate the button using the string and benefit from the collection methods and fast enumeration. Apple's documentation for Interface Builder indicates that the "name" in IB is used to assist identifying objects in IB which is helpful for translation.
I have several instances of a UIControl class Foo being instantiated, one instance corresponding to each cell in a UITableView. The Class has:
BOOL selected;
UIImageView *imageView;
UIImage *imageOne;
UIImage *imageTwo;
I've assigned each instance a tag:
foo.tag = indexPath.row;
I would now like to reference the UIImageView.image for a (or several) specific instance(s) by its tag to switch it to the other image.
In my search I've seen things like classes being assigned tags using initWithTag (I assume they're assigning tags)...
SomeClass *someClass = [[SomeClass alloc]initWithTag:1 ...
[someArray addObject: [[SomeClass alloc]initWithTag:2 ...
[someArray addObject: [[SomeClass alloc]initWithTag:3 ...
...but I haven't seen how they are later referenced by that tag.
I have seen a reference to getChildByTag which had promise, but I can't find it in the documentation or examples (maybe not iphone).
Does anyone know how reference the imageView.image within an instance using the instance's tag? (the imageView doesn't have a tag)
Thanks
Let me be a bit more specific. As each instance of Foo is set up in the UITableViewCells I use addTarget
[self addTarget: self action: #selector(switchImage:) forControlEvents: UIControlEventTouchDown];
Then I have this method to switch the images:
- (void) switchImage:(id)sender
{
selected = !selected;
imageView.image = (selected ? imageOne : imageTwo);
// self.tag in here is the indexPath.row from the foo.tag initially assigned
// NSLog(#"switchImage:%#",sender);
}
This works perfectly. I click on the image and the image switches. But in the entire tableView, I only want 1 imageOne all the rest to be imageTwo so I want a way to first turn off all images and then turn on the one. I therefore felt that I could to loop thru all of the instances of Foo using the tag to either somehow directly change the imageView.image or run switchImage in each of the instances to turn off each cell's image.
Lastly, when I look at sender via NSLog, I see that each Foo has a different address so I was wondering if something like allTargets (Foo is a UIControl) would allow me to get to all of the switchImage methods.
I'm pretty deep into this rabbit hole but I'll certainly start over if necessary.
I agree that there are other ways to deal with this, but you could add all your Foo objects to an array or set and iterate through them until you find the one with the tag you want, then access the imageview in the usual way.
If I'm getting you right, you try to access Foo objects (globally) by the tag you assigned to them before. There's no way to do that and it's not what the tag property of UIViews was designed for.
You have to use another way to access your Foo objects. There are uncountable ways of setting up a way to access objects. Since you would usually access them from their data source or view controller, I’d add a dictionary with there. You could also use UITableView's visibleCells property to only change images on cells that are actually displayed.
I feel a bit lost, but how about [UIView viewWithTag:]?
UIView instances have a tag property. You can access them from their superview using:
UIView *view = [superview viewWithTag:n];
Say I have 4 UIViews, made in IB, all with the tag property = 2
When I get a view with:
UIView *thisView = (UIView*)[self.view viewWithTag:2];
What is the criterion for retrieving that UIView since several have the same .tag value?
Is it:
random
the first one created
the view with the lowest index in it's superview
something else
Its weird but the view that is added first will be returned if you try to get the views among the views with same tag. You can check it in this way too.
NSLog(#"%#",[[self.view viewWithTag:custTag] class]);
Hope this helps.
If you use Interface Builder, it depends which order you use. In my case, UIActivityIndicator will be my result, and not UIWebView or UIButton with the same tag.
if I had to guess, I would assume that it would be almost-random. as in, you'll probably get some amount of consistency, but every so often it'll be something completely different (heh).
I'd say use different tags for them?