"Learn Swift from scratch" learning notes (day)--cocoa Touch design mode and application goals and actions

Source: Internet
Author: User
Tags uicontrol

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

The Target and action (action) are medium event handling mechanisms for iOS and OS X app development.

Questions raised

is a buttonlabelsample case design prototype that contains a label and a button that, when clicked, replaces the label text with HelloWorld from the original label.

The first problem to be solved in the Buttonlabelsample case is: who is responsible for responding to events after button click events? Who is handling the event? To answer this question, You can open the Buttonlabelsample case Storyboard file Main.storyboard,,ok button is defined in the storyboard file Main.storyboard, response events, and handling events should be implemented in program code Viewcontroller.swift. So how do I associate the OK button click event with the event handling code in Viewcontroller.swift? Different computer languages are implemented in different ways, in iOS and OS X application development through the goal and action mechanism to implement event processing.

Solution Solutions

Controls such as buttons are inherited from the Uicontrol class, with some advanced events, and the goal and action mechanism is to associate a specially-controlled event with a method in the View controller (or view), a process called defining an action event. A "target" is a response to an event object, which is typically a view controller (or view) in order to facilitate access to other control states. An "action" is an event of a control.

In the Buttonlabelsample case, the button is defined in the storyboard file (or Xib file), and the Response button click event (action) is defined in the method of the view controller (the target), as shown in, by defining the action event mode to connect the object to the action.

There are two ways to achieve a connection between a target and an action: Interface Builder Connection implementation and programmatic implementation.

1. Interface Builder Connection Implementation

The Interface Builder connection is a storyboard or xib file that is wired and realistic.

2. Programming Implementation

The programming implementation is implemented through the Uicontrol class Addtarget (_:action:forcontrolevents:) method, the main code is as follows:

classViewcontroller:uiviewcontroller {override func Viewdidload () {Super. Viewdidload () Self.view.backgroundColor=Uicolor.whitecolor () let screen=Uiscreen.mainscreen (). Bounds; Let Labelwidth:cgfloat= 90Let labelheight:cgfloat= 20Let labeltopview:cgfloat= 150Let label=UILabel (Frame:cgrectmake (screen.size.widthê-Labelwidth)/2, Labeltopview, Labelwidth, Labelheight)) Label.text= "Label"//font left and right in the playlabel.textalignment= . Center Self.view.addSubview (Label) let button= UIButton (Type:UIButtonType.System)//Create a UIButton objectButton.settitle ("OK", ForState:UIControlState.Normal) let Buttonwidth:cgfloat= 60Let buttonheight:cgfloat= 20Let buttontopview:cgfloat= 240Button.frame=CGRectMake ((screen.size.widthê-Buttonwidth)/2, Buttontopview, Buttonwidth, Buttonheight) Button.addtarget (Self, Action:"OnClick:", Êforcontrolevents:uicontrolevents.touchupinside) Self.view.addSubview (Button)} Func OnClick (sender:anyobject) {NSLog ("OK button OnClick.")    }        ...}

The above code creates and sets the UIButton object, where the UIButton object is created, the parameter type is the style of the Set button, the UIButton style:

    • Custom. The custom type. If you don't like the rounded button, you can use that type.
    • System. System default property, which indicates that the button has no border, before iOS 7 the button defaults to a rounded rectangle.
    • Detail Disclosure. Detail Display button, mainly used in the table view of the details of the display.
    • Info Light and Info Dark. These two are informational buttons that, like the detail display buttons, indicate that there is some information that needs to be displayed, or something that can be set.
    • ADD Contact. Add a contact button.

The code calls the Addtarget (_:action:forcontrolevents:) method, and the first parameter of the method is target, the event-handling object, in this case self; the second parameter of the method is the action, which is the method in the event-handling object.

The code is "OnClick:", the third parameter of the method is the event, and the Touchupinside event is a touch click event of the button.

If you call the following parameter-free method:

Func OnClick () {

}

The calling code is as follows:

Button.addtarget (Self, Action: "OnClick",        êforcontrolevents:uicontrolevents.touchupinside)

The difference is that the action parameter "OnClick" method name differs, and the colon of the action parameter method name implies that the method name should have several parameters. If the method to be called is the following 3 parameter forms:

Func OnClick (Sender:anyobject, Forevent event:uievent) {}

Then the calling code is as follows:

Button.addtarget (Self, Action: "Onclick:forevent:",        êforcontrolevents:uicontrolevents.touchupinside) 

where "onclick:forevent:" is the calling method name, and the OnClick means that the method name is also, forevent represents the external parameter name for the second parameter.

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php

"Learn Swift from scratch" learning notes (day)--cocoa Touch design mode and application goals and actions

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.