views:

75

answers:

3

Hi,

I have a view with several subviews which in turn have several subviews. If I fire an event, say touches ended, in one of the bottom level views, how can I see what that event generating view was in the root view or second level view?

Additionally any idea how to catch that event and then release the mid level view which would in theory take all of it's subviews with it so that i could then draw a brand new view to the screen. Trying but calling release isn't updating the screen, tried = nil but that caused memory leaks on iPhone. Any ideas>

Thanks // :)

+1  A: 

To find the view furthest down that was tapped:

tappedView = [view hitTest:point withEvent:event]

To take the view out, with its children, use:

[view removeFromSuperview];
mahboudz
A: 

For the second part, you want to call [view removeFromSuperview], which will have the side-effect of releasing the view.

Martin Gordon
+3  A: 

The second argument to touches<Began|Moved|Ended|Cancelled>:withEvent: is an instance of UIEvent. UIEvent can be asked for the touches that belongs to a particular view. This is useful for acting upon touches for a particular view. For example:

-(void)touchedEnded:(NSSet*)touched withEvent:(NSEvent*)event;
{
  NSSet* aParticularViewsTouches = [event touchesForView:aParticularView];
  // DO some stuff
}

Subviews are added to the view hierarchy by calling for example addSubview: or insertSubview:atIndex: on the parent. Removing a subview is not quite as obvious, you have to call removeFromSuperview on the childview. As an example here I add and then remove a childview:

[parentView addSubview:childView];
[childView removeFromParentView];

So if you would like to remove a child view with a known index, but you do not have a reference to it, then you could do this:

-(void)removeChildviewAtIndex:(NSUInteger)index;
{
  UIView* childView = [[parentView.childviews objectAtIndex:index];
  [childView removeFromParentview];
}

If you add this method in a category to UIView, then just substitute parentView for self, and it works.

PeyloW