Subview event response and methods to obtain Subview in IOS development _ios

Source: Internet
Author: User
Tags reserved uikit

Event response for Subview
In the view hierarchy, Subview can be displayed outside of the frame area of its parent view by default, and you can restrict the display area of Subview by setting the ClipToBounds property to Yes. But when the touch is passed in each UIView, the area is limited to the frame of the view, which contains two messages: 1, it is easy to understand that operations outside the frame of the current view are not passed into the view. 2. If the touch event occurs outside the frame of the current view, all subview of the view will not receive the message again. This is usually easy for us to ignore, and many strange problems are caused by this.

Here's a small example of the custom view Code as follows:

Copy Code code as follows:

SvTestClipSubviewEvent.h


//

SvTestClipSubviewEvent.h

Svuiviewsample

//

Created by Maple on 3/19/12.

Copyright (c) smileevday. All rights reserved.

//

By default, Subview can go beyond the frame of the parent view to display outside of the parent view

But the acceptance of the message returns due to the size limit of the parent view, which means that the subview of the parent view will not receive the message

Be sure to note in your program that the bottom of the current view is the one that fills the entire window's available area,

Otherwise, it will cause certain areas to have a button but not the problem.

#import <UIKit/UIKit.h>

@interface Svtestclipsubviewevent:uiview

@end

Copy Code code as follows:

//
Svtestclipsubviewevent.m
Svuiviewsample
//
Created by Maple on 3/19/12.
Copyright (c) smileevday. All rights reserved.
//

#import "SvTestClipSubviewEvent.h"

@interface Svtestclipsubviewevent ()

-(void) Btnaction: (uibutton*) btn;

@end

@implementation Svtestclipsubviewevent

-(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;
}


/*
Only override drawrect:if you perform custom drawing.
An empty implementation adversely affects performance during.
-(void) DrawRect: (cgrect) rect
{
Drawing Code
}
*/

-(void) Btnaction: (uibutton*) Sender
{
NSLog (@ "HI, tap button%@", [sender Titleforstate:uicontrolstatenormal]);
}
@end


Add the following test code to the Viewcontroller of your program:

Copy Code code as follows:

Svtestclipsubviewevent *testclipsubview = [[Svtestclipsubviewevent alloc]initwithframe:cgrectmake (100, 100, 150, 150) ];
[Self.view Addsubview:testclipsubview];
[Testclipsubview release];

Run to see the following interface:

Get Subview
There are usually two ways we can manipulate subview in the view hierarchy: 1, keep a subview reference, and then manipulate the Subview in the class, but be careful to add the code for the memory maintenance at the appropriate place and manually release it before exiting. 2, set Subview tag, so that when to use, through the Viewwithtag to obtain the corresponding Subview, this method is relatively concise, also do not have to maintain their own memory.

Viewwithtag: Usually uses the depth traversal precedence algorithm, returns the first tag and the given tag equal subview. This leads to a view that when multiple subview of a view are identical, we may not have the way we want it.

The following is a small example to verify that the code is as follows:

Copy Code code as follows:

//
SvTestViewTag.h
Svuiviewsample
//
Created by Maple on 3/18/12.
Copyright (c) smileevday. All rights reserved.
//
View performs a depth-first traversal algorithm based on the tag's acquisition of Subview
Returns the first tag and the same child view as the request tag
Look up from the subviews, the lowest priority to find

#import <UIKit/UIKit.h>

@interface Svtestviewwithtag:uiview

@end

Each subview in the


example is a uilabel, and the appropriate content is set. The implementation of the response function of the button: first hides all types of Uilabel Subview (excluding the UIButton, because the button needs to always show), and then according to the specified tag to get the corresponding Subview, The hidden property of the Subview and its superview is no. This ensures that when the button is clicked, only the first tag is displayed and the specified tag is equal to the subview.
&NBSP
In order to verify the principle of Viewwithtag getting subview:
 
First I added two subview1 and SUBVIEW11 in Subview12 for 11. Run the program, when we click the "Show Tag 11" button, the screen will display "SUBVIEW11", rather than "SubView12." And no matter how many times you click the button, always show only "SUBVIEW11". This can be seen at the same level to get subview when the search order for index from small to large principle, that is located in the relative lower levels will be first found.
 
Second, I also added in the subview1 tag are 13 of the subview13, while added to the view of the tag is also 13 Subview2, run the program click "Show Tag 13" button, the screen will display " SubView13 ", not" SubView2 ". This verifies that Viewwithtag follows the principle of depth-first traversal when searching, that it first looks at the bottom view and recursively queries its subview.
 
Fully two points we can see that viewwithtag get the basic principle of subview, that is, follow the depth first, lower priority two principles.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.