IOS Interview Questions:

IOS Basic Interview Questions 1

IOS Interview Collection + Answer (1)

IOS Interview Collection + Answer (2)

IOS Interview Collection + Answer (3)

IOS Interview Collection + Answer (4)

IOS Interview Collection + Answers (5)

Common iOS Development Interview Questions

A learning route for iOS developers

The Cocoa Touch framework uses a lot of delegation. According to the Apple documentation, delegate is one form of delegation used by the Cocoa Touch framework.

Understand the preparation required for delegating

(I) Agreement

Objective-c protocol, like C++ abstract class, JAVA interface. Its specific definition is as follows

1.  @protocol MyButtonDelegate <NSObject>
2.   
3.  @optional
4.  - (void) didPressButton;
5.   
6.  @end
Copy the code

@protocol is the protocol keyword, MyButtonDelegate is the protocol name, didPressButton is the method in the protocol.

(2) ID type

The id type can be thought of as a pointer to any object,

It is defined as:

1.  typedef struct objc_class *Class;
2.  typedef struct objc_object {
3.     Class isa;
4.  } *id;
Copy the code

(3) Adapter mode

In design pattern, there is no delegate pattern. But there is an adapter mode, the adapter mode can be understood in this way, if we go to Hong Kong, want to give my computer power supply, found that some of the plug board how also can not be plugged (Hong Kong is the use of British plug), can only plug a converter first, in the computer to the converter. So that’s the adapter pattern in life, most delegate patterns, that’s the implementation of the object adapter function,

(4) Examples

We want to implement a uiButton-like component that we build ourselves. First of all, we inherit Mybutton from UIView, and we have a problem here, we don’t know who’s going to use Mybutton in the future, but we do know that everyone who uses Mybutton needs to get a message when that button is pressed, To tell the object (Adaptee) that uses myButton that myButtton is pressed.

At this time, our adaptation is as follows:

1.  @protocol MyButtonDelegate <NSObject>
2.   
3.  @optional
4.  - (void) didPressButton;
5.   
6.  @end
Copy the code

My Adapter is Mybutton, and it goes through

@property (nonatomic,assign) id <MyButtonDelegate>delegate;
Copy the code
1. if (delegate && [delegate respondsToSelector:@selector(didPressButton)]) { 2. [delegate performSelector:@selector(didPressButton)]; 3.}Copy the code

To implement a call to an Adaptee, which can be any object, in this case, the RootViewController (which implements the protocol)

(5) In-depth understanding of the delegation mode

The implementation of the delegate pattern, which can also be implemented through blocks, is only suitable for code that executes a one-time callback.

The original: blog.csdn.net/xunyn/artic…