Objective-c method in the back

Source: Internet
Author: User

Objective-c method in the back

Method calls in the Objective-c

OC similar to c+, can not avoid the internal various means of communication, function calls, variable transmission, etc., can not avoid a variety of back-off and so on.

One, class direct call or obj call

OC method is divided into two main, a "-" the beginning of the example method, this method, as the name implies, must have a concrete instance of the object to be removed.

such as: [obj methodName];

The other is a static method that begins with "+", which is usually called without creating an object, and can be implemented directly through [ClassName Staticmethodname].

such as: [ClassName staticmethodname];

Second, the agent design mode

Single from the design mode, may feel that the agent is a very complex thing, in fact, it is simply to let a certain obj to keep a pointer, at the critical moment to deliver a certain part of the task is left to the pointer to the object to complete, this method is more common (can do data transfer, You can also do a method of returning (calling the other method in the two class).

c+ In addition to keeping a pointer, at the time of use, the pointer to the address of an object, can be obtained through this pointer to the corresponding object to do the corresponding task, the c+ feature is that the time of the method call must be clear that the object belongs to the specific class of class, Otherwise, the system will say that it cannot find a corresponding method, directly lead to compilation errors, which are related to his characteristics. So there are some corresponding strong and weak type conversions. If the child type object is safe to the parent class, the reverse is very dangerous.

The agent in iOS is basically a common protocol, the class of the Protocol, and the class three parts to implement the protocol. IOS delegate are similar to C + +, but there are differences. The delegate declaration is typically of type ID, and the ID type is similar to C + + (void *) and can point to any object. First the proxy class wants to call someone's method at some point, the proxy class must define a corresponding protocol, then the proxy class can follow its own protocol template, the implementation of the final protocol is done by other classes. The end result is that the proxy class does not implement the method, but can be transferred to the method, the implementation of the Protocol class implementation of the corresponding method, but generally do not call the method directly. The proxy class and the implementation proxy class at the time of the call, by a pointer in the middle to connect up and down to a control chain role.

Note: The time in the proxy class to invoke the method in the protocol as far as possible according to the rules of objective-c, avoid unnecessary warning problems, define the corresponding protocol, in the proxy class according to this rule to go,

Such as:

@protocol jdfooterloadingviewdelegate;

@interface Jdfooterloadingview:uiview {

}

A1. @property (nonatomic,assign) id<jdfooterloadingviewdelegate> delegate;

You can use generics

@end

@protocol jdfooterloadingviewdelegate <NSObject>

@optional

-(void) Footerloadingviewbackaction: (ID) sender;

@end

Call Time:

A2. if ([Self.delegate respondstoselector: @selector (footerloadingviewbackaction:)]) {

[Self.delegate Footerloadingviewbackaction:sender];

If there is no declaration to define protocol, do not judge the time in the method to display a warning, although the OC runtime determines the specific type, does not cause crash, but compile time generates a warning. It is recommended to follow the rules.

The Jdfooterloadingviewdelegate protocol is done by initializing the class that uses Jdfooterloadingview.

Post-note:

There are some classes in the project, only one delegate pointer is left, and then a warning is taken at A2, which can only be avoided with pointers to eliminate the warning. (Cast self.delegate to the class type that implements the protocol)

Ii. Closures (block)

Viewcontrollera---Viewcontrollerb, Viewcontrollerb need to call immediately after completion of the operation of Viewcontrollera, this time in addition to the use of closures outside the agent is also a good choice, Define a block in the Viewcontrollerb,

Jump Time in Viewcontrollera

viewcontrollera.m

Viewcontrollerb *VCB =[[Viewcontrollera alloc] init];

Vcb.complateblock = ^{

Do something;

};

Closure implementation in the Viewcontrollera, the real call in Viewcontrollerb, this means can also solve the problem.

Closures and the advantages and disadvantages of the agent, the closure of the idea is more clear, do not define protocol implementation methods and other cumbersome operations, but the closure itself is an object, again increase the management of the object life cycle, but also to avoid repeated references and so on.

Third, notification mode

Nsniotificationcenter, the above problem throws a notification in Viewcontrollerb, Viewcontrollera increases the listening, disadvantage (logical breakpoint).

KVO, similar to the nsniotificationcenter phase.

Objective-c method in the back

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.