"Learn Swift from scratch" study notes (Day 65) - Cocoa Touch design mode and application selector

"Learn Swift from scratch " study notes ( Day 65 ) - Cocoa Touch design mode and application selector

 

Original article, welcome to reprint. Please indicate: Guan Dongsheng's blog

 

Use the UIControl class addTarget(_:action:forControlEvents:) method to associate the target with the action . The sample code is as follows:

button.addTarget(self, action: "onClick:",
            forControlEvents: UIControlEvents.TouchUpInside)

 

The action parameter "onClick:" is actually the selector ( Selector ).

 

Questions raised

Any binding that can defer the method call to the runtime, the method caller does not need to know what the method to call is at compile time, this can reduce the coupling between the caller and the callee, so the language is very flexible . In the C language, a function pointer technology is provided. Both Objective-C and Swift languages ​​provide the selector ( Selector ) type, which is an object-oriented replacement technology for the C language function pointer.

The selector is the key to the implementation of method invocation in the target action, notification and delegate patterns in Cocoa and Cocoa Touch .

 

solution

The selector in Objective-C is of the SEL data type. It is called using the @selector() statement. The Objective-C sample code for calling the onClick: method is as follows:

SEL selector = @selector(onClick:);
[button addTarget:self action: selector
          forControlEvents: UIControlEventTouchUpInside];

 

Although the SEL data type is not provided in Swift , the Selector structure is provided, and the Selector instance is constructed by the method name string. The sample code is as follows:

button.addTarget(self, action: Selector("onClick:"),

            Ê forControlEvents: UIControlEvents.TouchUpInside)

Calling a method through a selector, the key is the method name, which has a certain pattern. The bottom line is that it comes from the naming rules of Objective-C multi-parameter methods. The colon in the method name implies that the method name should have several parameters. Let's look at a few examples:

    

//The selector is "onClick:"
    func onClick(sender: AnyObject) {
        NSLog("onClick:")
    }
   
    //The selector is "onClick:forEvent:"
    func onClick(sender: AnyObject, forEvent event: UIEvent) {   
        NSLog("onClick:forEvent:")
    }
   
    //The selector is "onClickWithExtSender:forEvent:"
    func onClick(extSender sender: AnyObject, forEvent event: UIEvent) {
        NSLog("onClickWithExtSender:forEvent:")
    }
 

 

For the needs of data encapsulation, we will add private in front of the method to make it a private method. The code is as follows.

   

private func onClick(sender: AnyObject) {
        NSLog("onClick:")
    }

 

But the following error occurs when calling this method:

unrecognized selector sent to instance 0x7f7f81499b10'

 

This error means that the method specified by the selector was not found, that is, the onClick: method was not found. The correct way is to add in front of the method@objc attribute annotation, which indicates that the selector is called in the objc runtime runtime environment.

    

//The selector is "onClick:"
    @objc private func onClick(sender: AnyObject) {
        NSLog("onClick:")
    }
 
 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058306&siteId=291194637