Home>

subview event response

In the view hierarchy,By default, a subview can be displayed outside the frame area of ​​its parent view.By setting the cliptobounds property to yes, you can limit the display area of ​​the subview.But when touch is passed in each uiview,The time zone is limited to the frame of the view, which contains two information:1. Operations performed outside the frame of the current view will not be passed to the view, which is easy to understand.2. If the touch event occurs outside the frame of the current view, all subviews of the view will no longer receive the message.This is often easily overlooked by us,Many strange problems are caused by this.

Please see a small example below,The custom view code is as follows:

svtestclipsubviewevent.h
</p>
<p>
//
</p>
<p>
//svtestclipsubviewevent.h
</p>
<p>
//svuiviewsample
</p>
<p>
//
</p>
<p>
//created by maple on 3/19/12.
</p>
<p>
//copyright (c) 2012 smileevday. all rights reserved.
</p>
<p>
//
</p>
<p>
//By default,The subview can exceed the frame of the parent view, that is, it can be displayed outside the parent view
</p>
<p>
//But the acceptance and return of the message is due to the size limitation of the parent view,That is, the subview out of the parent view will not receive the message
</p>
<p>
//In the program, we must pay attention to that the bottom layer of the current program view is full of the available area of ​​the entire window,</p>
<p>
//Otherwise, it will cause problems in some areas where there are buttons but they are not clicked.
</p>
<p>
#import<uikit/uikit.h>
</p>
<p>
@interface svtestclipsubviewevent:uiview
</p>
<p>
@end
//
//svtestclipsubviewevent.m
//svuiviewsample
//
//created by maple on 3/19/12.
//copyright (c) 2012 smileevday. all rights reserved.
//
</p>
<p>
#import "svtestclipsubviewevent.h"
</p>
<p>
@interface svtestclipsubviewevent ()
</p>
<p>
-(void) btnaction:(uibutton *) btn;
</p>
<p>
@end
</p>
<p>
@implementation svtestclipsubviewevent
</p>
<p>
-(id) initwithframe:(cgrect) frame
{
 self=[super initwithframe:frame];
 if (self) {
 //initialization code
 self.backgroundcolor=[uicolor redcolor];
 uibutton * testoutbtn=[uibutton buttonwithtype:uibuttontyperoundedrect];testoutbtn.frame=cgrectmake (-80, -50, 70, 30);
 [testoutbtn addtarget:self action:@selecto (btnaction:forcontrolevents:uicontroleventtouchupinside];
 [testoutbtn settitle:@"i" m out "forstate:uicontrolstatenormal];
 [self addsubview:testoutbtn];
 uibutton * testinbtn=[uibutton buttonwithtype:uibuttontyperoundedrect];testinbtn.frame=cgrectmake (20, 30, 70, 30);
 [testinbtn settitle:@"i" m in "forstate:uicontrolstatenormal];
 [testinbtn addtarget:self action:@selector (btnaction:forcontrolevents:uicontroleventtouchupinside];
 [self addsubview:testinbtn];
 }
 return self;
}
</p>
<p>
/*
//only override drawrect:if you perform custom drawing.
//an empty implementation adversely affects performance during animation.
-(void) drawrect:(cgrect) rect
{
 //drawing code
}
* /
</p>
<p>
-(void) btnaction:(uibutton *) sender
{
 nslog (@ "hi, you tap button%@", [sender titleforstate:uicontrolstatenormal]);
}
@end

Add to Add the following test code to the program's viewcontroller:

svtestclipsubviewevent * testclipsubview=[[svtestclipsubviewevent alloc] initwithframe:cgrectmake (100, 100, 150, 150)];
[self.view addsubview:testclipsubview];
[testclipsubview release];

Run to see the following interface:

Get subview

Generally, we can operate the subview in the view level in two ways:1. Keep a reference to the subview, and then operate the subview through the reference in the classBut be careful to add memory-maintained code where appropriate,Release manually before exiting.2. Set the tag of the subview so that when it is used later,Get the corresponding subview through viewwithtag, this method is more concise,You don't have to maintain memory yourself.

Viewwithtag:usually use depth traversal first algorithm,Returns the first subview with the same tag as the given tag. This leads to a situation when the tags of multiple subviews of a view are the same,The view we get through this method may not be what we want.

Let's verify it with a small example,code show as below:

//
//svtestviewtag.h
//svuiviewsample
//
//created by maple on 3/18/12.
//copyright (c) 2012 smileevday. all rights reserved.
//
//The view executes the depth-first traversal algorithm when the subview is obtained according to the tag
//return the first child view with the same tag as the requested tag
//Search from subviews, the lowest one is found first
</p>
<p>
#import<uikit/uikit.h>
</p>
<p>
@interface svtestviewwithtag:uiview
</p>
<p>
@end

Each subview in the example is a uilabel, and the corresponding content is set.The implementation idea of ​​the response function of the button:first hide all subviews of type uilabel (excluding uibutton, because the button needs to be displayed all the time), and then get the corresponding subview according to the specified tag. The hidden attribute of this subview and its superview is no. This can ensure that when the button is clicked, only the first subview with the same tag as the specified tag is displayed.

verification In order to verify the principle of viewview with subview:

First I added two subview11 and subview12 with 11 tags in subview1. Run the program,When we click the "show tag 11" button, "subview11" will be displayed on the screen instead of "subview12". And no matter how many times you click the button,Only "subview11" is always displayed. In this way, it can be seen that when the subview is obtained in the same level, the search order is the index from small to large.That is, the one located relatively below will be found first.

Secondly, I also added subview13 with a tag of 13 in subview1, and added subview2 with a tag of 13 to the view. Run the program and click the "show tag 13" button. The screen will display "subview13" instead of " subview2 ". This can verify that viewwithtag follows the principle of depth-first traversal when searching,That is, it will first find the lowest view and recursively query its subviews.

To sum up the two points above, we can see the basic principles of viewwithtag to obtain a subview.I.e. follow depth first,The lower layer takes precedence over two principles.

ios
  • Previous DropDownList adds client drop event action
  • Next DropDownList set client event ideas