Target-Action in iOS Design Pattern

Source: Internet
Author: User

Target-Action in iOS Design Pattern
Target-Action (Target-action) mode (aims to decouple the code, reduce the correlation between the code and the code, and facilitate future development and maintenance) Target-Action-a button used in this design mode, and other controls to turn user interaction into code, so that the program can execute; Target-action: easy to understand, that is, an object contains elements that generate a message expression, when an event is identified, these elements are put together to form a message and send the message. Some concepts are abstract and fuzzy. The Target-Action mode is interpreted using an example below.
First, let's take a look at the following: Release/release/zcrxatv46ubnu7frh1_ldc1by3rpb24oudi8/release = "p1"> + (UIColor *) randomColor.

{

Return [selfcolorWithRed: arc4random () % 256/255 .0 green: arc4random () % 256/255 .0 blue: arc4random () % 256/255 .0 alpha: 1];

}


// Create three UIView objects

Code:

// MHTViewController. m

// LessonTragetAction

// Copyright (c) 2014 Summer. All rights reserved.

# Import "MHTViewController. h"

# Import "CustomView. h"

# Import "UIColor + RandomColor. h"

# Define kCyanView_Frame CGRectMake (100,130,120,120)

# Define kGreenView_Frame CGRectMake (15,260,120,120)

# Define kGrayView_Frame CGRectMake (185,260,120,120)

@ InterfaceMHTViewController ()

@ End

@ Implementation MHTViewController

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [super initWithNibName: nibNameOrNilbundle: nibBundleOrNil];

If (self ){

// Custom initialization

}

Returnself;

}

-(Void) viewDidLoad

{

[SuperviewDidLoad];

// Do any additional setup after loading the view.

Self. view. backgroundColor = [UIColoryellowColor];

// View1

CustomView * greenView = [[CustomViewalloc] initWithFrame: kGreenView_Frame];

GreenView. backgroundColor = [UIColorgreenColor];

GreenView. layer. cornerRadius = 60;

[GreenViewaddTarget: selfaction: @ selector (changeSuperviewColor :) forControlEvents: UIControlEventTouchUpInside];

GreenView. layer. masksToBounds = YES;

[Self. viewaddSubview: greenView];

[GreenViewrelease];

// View2

CustomView * cyanView = [[CustomViewalloc] initWithFrame: kCyanView_Frame];

CyanView. backgroundColor = [UIColorcyanColor];

[Self. viewaddSubview: cyanView];

CyanView. layer. cornerRadius = 60;

[CyanViewaddTarget: selfaction: @ selector (changeSelfColor :) forControlEvents: UIControlEventTouchDown];

CyanView. layer. masksToBounds = YES;

[CyanViewrelease];

// View3

CustomView * grayView = [[CustomViewalloc] initWithFrame: kGrayView_Frame];

GrayView. backgroundColor = [UIColorgrayColor];

[Self. viewaddSubview: grayView];

[GrayViewaddTarget: selfaction: @ selector (changeSelfLocation :) forControlEvents: UIControlEventTouchUpInside];

GrayView. layer. cornerRadius = 60;

GrayView. layer. masksToBounds = YES;

[GrayViewrelease];


NSArray * titles = @ [@ "change color of parent View", @ "change color", @ "change location"];

For (int I = 0; I <3; I ++ ){

UILabel * lable = [[UILabelalloc] initWithFrame: CGRectMake (120,120,)];

Lable. layer. cornerRadius = 60;

Lable. layer. masksToBounds = YES;

Lable. text = titles [I];

Lable. font = [UIFontfontWithName: @ "EuphemiaUCAS" size: 15];

Lable. textAlignment = NSTextAlignmentCenter;

Lable. backgroundColor = [UIColorclearColor];

If (0 = I ){

[GreenViewaddSubview: lable];

} Elseif (1 = I ){

[CyanViewaddSubview: lable];

} Elseif (2 = I ){

[GrayViewaddSubview: lable];

}

[Lablerelease];

// Set the split line

UILabel * hintLine = [[UILabelalloc] initWithFrame: CGRectMake (50,80, 220,2)];

HintLine. backgroundColor = [UIColorgrayColor];

HintLine. layer. cornerRadius = 5;

HintLine. layer. masksToBounds = YES;

[Self. viewaddSubview: hintLine];

[HintLinerelease];

// Set the prompt content of the Function

UILabel * hintLable = [[UILabelalloc] initWithFrame: CGRectMake (50,60, 220,20)];

HintLable. textAlignment = NSTextAlignmentCenter;

HintLable. text = @ "text display :";

HintLable. font = [UIFontfontWithName: @ "AlNile" size: 12];

[Self. viewaddSubview: hintLable];

[HintLablerelease];

// CopyrightLine (copyright)

UILabel * copyrightLine = [[UILabelalloc] initWithFrame: CGRectMake (20,520,280, 2)];

CopyrightLine. backgroundColor = [UIColorgrayColor];

CopyrightLine. layer. cornerRadius = 5;

CopyrightLine. layer. masksToBounds = YES;

[Self. viewaddSubview: copyrightLine];

[CopyrightLinerelease];

UILabel * copyrightLable = [[UILabelalloc] initWithFrame: CGRectMake (20,480,280, 20)];

CopyrightLable. textAlignment = NSTextAlignmentCenter;

CopyrightLable. text = @ "Copyright? 2014 summer ";

CopyrightLable. font = [UIFontfontWithName: @ "AlNile" size: 12];

[Self. viewaddSubview: copyrightLable];

[CopyrightLablerelease];

UILabel * copyrightLable1 = [[UILabelalloc] initWithFrame: CGRectMake (20,500,280, 20)];

CopyrightLable1.textAlignment = NSTextAlignmentCenter;

CopyrightLable1.text = @ "summer2014mht@sina.com ";

CopyrightLable1.font = [UIFontfontWithName: @ "AlNile" size: 12];

[Self. viewaddSubview: copyrightLable1];

[CopyrightLable1release];

}

// Modify the color of the parent View

-(Void) changeSuperviewColor :( CustomView *) view

{

View. superview. backgroundColor = [UIColorrandomColor];

}

// Modify its own color

-(Void) changeSelfColor :( CustomView *) view

{

View. backgroundColor = [UIColorrandomColor];

}

// Modify its location

-(Void) changeSelfLocation :( CustomView *) view

{

View. center = CGPointMake (arc4random () % (260-60 + 1) + 60, arc4random () % (420-60 + 1) + 60 );

}

Second build Target-Action

// CustomView. h

// LessonTragetAction

// Copyright (c) 2014 Summer. All rights reserved.

# Import

@ Interface CustomView: UIView

// Creation Method

-(Void) addTarget :( id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents;

@ End

//*********************

// CustomView. m

// LessonTragetAction

// Copyright (c) 2014 Summer. All rights reserved.

# Import "CustomView. h"

# Import "UIColor + RandomColor. h"

@ InterfaceCustomView ()

{

Id _ target; // target

SEL _ action; // action

UIControlEvents _ controlEvents;

}

@ End

@ Implementation CustomView


-(Id) initWithFrame :( CGRect) frame

{

Self = [super initWithFrame: frame];

If (self ){

// Initialization code

}

Returnself;

}

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

{

If (UIControlEventTouchDown ==_controlevents ){

// When the current view is exposed to a touch event, it is handled by the target.

[_ Targetpolicmselector: _ actionwithObject: self];

}

}

-(Void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event

{

}

-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event

{

If (UIControlEventTouchUpInside = _ controlEvents ){

[_ Targetpolicmselector: _ actionwithObject: self];

}

}

-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event

{

}

// Specify for the current view. After the view receives a response event, the target will respond using the action method.

-(Void) addTarget :( id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents

{

// Use instance variables to store external input parameters for ease of use in other methods.

_ Target = target;

_ Action = action;

_ ControlEvents = controlEvents; // trigger time

}

@ End




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.