original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
implement target and action Association use Uicontrol class AddTarget (_:action:forcontrolevents:) method, the sample code is as follows:
Button.addtarget (self,action: "OnClick:", ForControlEvents:UIControlEvents.TouchUpInside)
one of the Action Parameters "OnClick:" The fact is that selectors ( Selector ).
Questions raised
any ability to defer the binding of a method call to a run time, at compile time, the method caller does not need to know what method to invoke, which reduces the coupling between the caller and the callee so that the language is flexible. In the C language, which provides a function pointer technique, bothobjective-c and Swift languages provide selectors ( Selector ) type, which is C object-oriented substitution techniques for language function pointers.
selector in Cocoa and the Cocoa Touch The key to the invocation of a method in a target action, notification, and delegate mode.
Solution Solutions
objective-c Medium Selector is SEL data type, using @selector () statement invocation, calling OnClick: method of objective-c The sample code is as follows:
SEL selector = @selector (onClick:); [ Button AddTarget:selfaction:selector Forcontrolevents:uicontroleventtouchupinside];
Swift Although not provided in SEL data type, but instead provides a Selector struct, constructed by method name string Selector instance, the sample code is as follows:
Button.addtarget (Self,action:selector ("OnClick:"), ForControlEvents:UIControlEvents.TouchUpInside)
The method is called by the selector, the key is the method name, it has a certain regularity. The poor is rooted in the objective-c of the multiple parameter method named law. The colon of the method name implies that the method name should have several parameters, let's look at a few examples below:
//selector is "OnClick:" func onclick (Sender: anyobject) { nslog ("OnClick:") The } //selector is "onclick:forevent:" func onclick (sender: anyobject, foreventevent: uievent) { nslog ("onclick:forevent:") } //selector for "onclickwithextsender:forevent:" func OnClick (extsender sender: anyobject,forevent event: uievent) { nslog ("onclickwithextsender:forevent:") }
for the purposes of data encapsulation, we will add the above method to Private to make it a private method, as shown in the code below.
Private func OnClick (sender:anyobject) {NSLog ("OnClick:")}
However, the following error occurs when the method is called:
Unrecognized selector Sentto instance 0X7F7F81499B10 '
This error means that the method specified by the selector is not found, that is, no OnClick: method. It is a good practice to add @objc attribute comments before the method, which means that the selector is called in the objc runtime environment.
The selector is "onclick:" @objc private func OnClick (sender:anyobject) {NSLog ("onclick:")}
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
650) this.width=650; "Width=" "height=" "title=" Qrcode_for_gh_ca9759a5ef7f_258.jpg "style=" width:150px;height : 150px; "src=" http://s3.51cto.com/wyfs02/M02/7C/FF/wKioL1beULKTkydqAADC6S3kNXs503.jpg "border=" 0 "vspace=" 0 " Hspace= "0" alt= "wkiol1beulktkydqaadc6s3knxs503.jpg"/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1748676
"Learn Swift from scratch" learning notes (day)--cocoa Touch design mode and application Selector