Sixth day of the Swift project: Encapsulation of the intermediate release button and monitoring of click events

Source: Internet
Author: User
Tags event listener uikit

Import UIKit/* Summary: 1: Write classification to UIButton, create a new file Swiftfile, generally in order to distinguish the name is the name-extension, to invoke the UI control needs to import imports Uikit framework, and then to the System class write classification: Extension UIButton {}, which provides a method of a class method or constructor that encapsulates the business logic associated with the control in the interior of the taxonomy. 2: Encapsulation Method: Class method: All Start with class, class Func function name (parameter), return value type {Business logic code, return value}: Example: Class func Createbutton (Imagename:string, backgroundimagename:string), UIButton {}, where the function is declared, no arguments are added (), and the return value is write-only type. Classification without private and fileprivate decoration 3: Constructor method: Using the convenience constructor: Convenience init (_ imagename:string,bgimagename:string) convenience Convenient constructors, convenient constructors are typically used to extend the constructors of the class of the system by using the features of the traversal constructor 1. The traversal constructor is usually written in extension 2. Traversing the constructor init requires loading convenience 3. In the Traverse The constructor requires an explicit call to Self.init () convenience init (_ imagename:string,bgimagename:string) {//1: You must first call Self.init () Self.init ()/ /2: Set button let Higlingstr = "_highlighted"//2: Set the BG picture of the button with normal picture setimage (UIImage (Named:imagename), for:. Normal) SetImage ( UIImage (Named:imagename + higlingstr), for:. highlighted) SetBackgroundImage (UIImage (Named:bgimagename), for:. Normal ) SetBackgroundImage (UIImage (NAMED:BGIMAGENAME+HIGLINGSTR), For:. Highlighted)//3: Set the size of the button SizeToFit ()} NOTE: 1: Do not write the return value in the constructor, because the system returns 2 by default: The method that calls the class in the class can save self from calling 3: the first parameter of the system defaults to the internal parameter , from the second argument is the internal parameters and external parameters, if you want to make the parameters into internal parameters, then add _+ space in front of the parameters, modeled after the Apple System 2: Lazy loading: 1: Lazy load with the keyword lazy, with private or fileprivate to decorate 2: Lazy loading There are two ways of writing: 1: Direct initialization as Method 1 2: Using the closure lazy load: fileprivate lazy var composebtn = {return business logic + Return Value} (); , Apple recommends that the general definition variable use lazy loading 3: When defining a variable or property, you must give a definite initialization value or optional type, followed by assignment, or an error 1: Lazy load Publish button, provide two ways to create objects: 1: Class method, call Fileprivate with class point syntax Lazy var Composebtn:uibutton = Uibutton.createbutton (imageName: "Tabbar_compose_icon_add", Backgroundimagename: " Tabbar_compose_button ") 2: Object method gets instance of button: Fileprivate lazy var composebtn = UIButton (" Tabbar_compose_icon_add ", Bgima Gename: "Tabbar_compose_button") 3: Code writing: 1: When you define a property in a defined class, you can lazy-load the property, or you can define it as an optional type or assign a value directly to the property. However, when defining attributes, you need to consider using no private or fileprivate modifier 2: In the Viewdidload method encapsulation call, the encapsulated method is written in the extension extension of the class 3: If the extension extension involves some business logic processing is written into the classification of the System class (class method or constructor method), tool class, inheritance 2: In the classification or class to define the method, but also to consider using private or fileprivate decoration, Listener for button events: Composebtn.addtarget (Self, Action: #selector (MAINVIewcontroller.composebtnclic), for:. Touchupinside) Selector is wrapped with a #selector, the button clicks the event method with the class to invoke the method, enumeration with the. + enumeration value, The click Method of the button is also written in the extension extension 3: The Click event of the Package button: extension mainviewcontroller {@objc fileprivate func composebtnclic () {DLog  (Message: "Hello--word")} The nature of the event listener sends a message. However, sending a message is an OC feature that wraps a method into a @sel-class lookup method list--based on @sel to find the Imp pointer (function pointer)--execute function If Swift will declare a function as private,  Fileprivate then the function will not be added to the method list if you add @objc to the private front, the method will still be added to the method list*/classMainviewcontroller:uitabbarcontroller {//mark:-Lazy Load PropertiesFileprivate lazy var composebtn = UIButton ("Tabbar_compose_icon_add", Bgimagename:"Tabbar_compose_button")        //mark:-System callback function    Overridefunc viewdidload () {super.viewdidload ()//1: Create an intermediate publish buttonsetupcomposebtn ()}}//mark:-setting the UI interfaceextension Mainviewcontroller {///Set Publish buttonfileprivate func setupcomposebtn () {//1. Add composebtn to TabbarTabbar.addsubview (COMPOSEBTN)//2. Setting the locationComposebtn.center = Cgpoint (x:tabbar.center.x, Y:tabbar.bounds.size.height *0.5)                //3: Set monitoringComposebtn.addtarget (Self, Action: #selector (Mainviewcontroller.composebtnclic), for:. Touchupinside)} }//mark:-Click on the release buttonextension Mainviewcontroller {@objc fileprivate func composebtnclic () {DLog (message:"Hello--word")        }    }

Extension of the Publish button:

Import uikitextension UIButton {//Mark:-1: Providing class methods   classFunc Createbutton (imagename:string,backgroundimagename:string)UIButton {//1: Create buttonLet button =UIButton () let Higlingstr="_highlighted"        //2: Set the BG picture of the button with the normal pictureButton.setimage (UIImage (Named:imagename), for:. Normal) Button.setimage (UIImage (Named:imagename+ higlingstr), for:. Highlighted) Button.setbackgroundimage (UIImage (named:backgroundimagename), for:. Normal) Button.setbackgroundimage (UIImage (Named:backgroundimagename+HIGLINGSTR), for:. Highlighted)//3: Set the size of the buttonButton.sizetofit ()//4: Back button     returnButton}//mark:-2: Provides object methods: In the constructor's object method, self is the object that is currently calling the method, so there is no need to create the objectConvenience Init (_ imagename:string, bgimagename:string) {//1: Must first call Self.init ()Self.init ()//2: Set buttonLet Higlingstr ="_highlighted"                //2: Set the BG picture of the button with the normal pictureSetImage (UIImage (Named:imagename), for:. Normal) SetImage (UIImage (Named:imagename+ higlingstr), for:. Highlighted) SetBackgroundImage (UIImage (named:bgimagename), for:. Normal) SetBackgroundImage (UIImage (Named:bgimagename+HIGLINGSTR), for:. Highlighted)//3: Set the size of the buttonSizeToFit ()}}

Sixth day of the Swift project: Encapsulation of the intermediate release button and monitoring of click events

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.