Notification, block, notification block

Source: Internet
Author: User
Tags notification center

Notification, block, notification block

 

==========================================

Notification

==========================================

1. Notification (NSNotification)

// NSNotification notification class, which includes the NSNotificationCenter notification center class

Nsicationicationcenter * notification = [NSNotificationCenter defacenter center];

// Added a listener event, where run1 is the trigger event Method and @ "run" is the notification name.

[Notification addObserver: self selector: @ selector (run1) name: @ "run" object: nil];

 

// The method for sending notifications is called multiple times, and the corresponding response method (run1) is triggered multiple times)

[Notification postNotificationName: @ "run" object: nil];

// Delete the notification. to delete the notification, call removeObserver.

[Notification removeObserver: self name: @ "run" object: nil];

 

[Note] You must add a notification when using the notification. You must delete the notification when you do not need the notification. If you do not delete the notification, the notification will always exist.

 

2. [proxy and notification comparison]

// Proxy: Xiao Ming-> Xiao Gang-> Xiao Hong-> Xiao Li; Result: Xiao Li-> Xiao Hong-> Xiao Gang-> Xiao Ming

// Notification: James registered the notification. Result: Xiao Gang, Xiao Hong, and Xiao Li can send messages to Xiao Ming;

 

Iii. Notices

+ (Void) test

{

Xiaoming * xm = [[xiaoming alloc] init];

[Xm test1];

// [Note] You cannot add a listening method to the class method. This will cause the program to crash.

// Nsicationicationcenter * notification = [NSNotificationCenter defacenter center];

// [Notification addObserver: self selector: @ selector (run) name: @ "run" object: nil];

//

// [Xiaogang xgTest];

}

 

-(Void) test1

{

Nsicationicationcenter * notification = [NSNotificationCenter defacenter center];

[Notification addObserver: self selector: @ selector (run) name: @ "run" object: nil];

// [Note] When a listener event is added multiple times, the run method is triggered multiple times when a message is sent.

[Notification addObserver: self selector: @ selector (run) name: @ "run" object: nil];

 

[Notification addObserver: self selector: @ selector (run) name: @ "run" object: nil];

[Notification addObserver: self selector: @ selector (run) name: @ "run" object: nil];

// [Note] deleting a listener will delete all listeners with the corresponding name.

// [Notification removeObserver: self name: @ "run" object: nil];

// [Note] deleting a listener will delete all corresponding name listeners. The parameters following the object should be based on the parameters in the addObserver method.

[Notification removeObserver: self name: @ "run" object: @ ""];

[Notification addObserver: self selector: @ selector (run) name: @ "run" object: nil];

[Xiaogang xgTest];

}

==========================================

Block

==========================================

I. Recognize block

Block is also called a code block. It is a method starting with a ^ symbol. It is generally used for multithreading and network communication. Apple started to push block syntax from ios4

The block entity format is as follows:

^ (Input parameter list) {behavior subject (specific code implementation )}

 

// Declare a pointer function in C Language

Void (* cFunc) (void );

// Block in oc is similar to pointer Function

// Write a block Variable ocFunc

Void (^ ocFunc) (void );

1. block without Parameters

// [Note] block syntax. Execute the external syntax {} first. The internal syntax is executed only when the block function is called.

// Implements a block Function

// ^ (Input parameter list) {behavior subject (specific code implementation )}

// [Note] the block function ends;

OcFunc = ^ (void)

{

NSLog (@ "in blocks ");

};

NSLog (@ "befor blocks ");

// Block function call

OcFunc ();

NSLog (@ "after blocks ");

2. block with Parameters

// Int return value type; myblock1 block Function Name; int a, int B is the form parameter; ^ (int a, int B) {}; is the behavior subject

Int (^ myblock1) (int a, int B) = ^ (int a, int B)

{

Return a + B;

};

// Block function call

Int result1 = myblock1 (10, 20 );

NSLog (@ "result1 = % d", result1 );

// A function cannot contain another function. block is created

Func (10, 20 );

Int B = 8;

Int (^ myblock2) (int a) = ^ (int)

{

Return B +;

};

Int result2 = myblock2 (5 );

NSLog (@ "rusult2 = % d", result2 );

MyBlock myblock3 = ^ (int a, int B)

{

Return a + B;

};

Int result3 = myblock3 (90,8 );

NSLog (@ "rusult3 = % d", result3 );

// [Note] If you want to modify the external variables in the block, add the _ block modifier (with 2 underscores) to the external variables)

_ Block int sum;

Void (^ myblock4) (int a, int B) = ^ (int a, int B)

{

Sum = a + B;

};

Myblock4 (4, 5 );

NSLog (@ "sum = % d", sum );

// The value of A will be copied, and the operation inside the block is the copy part. Therefore, no matter how the external side modifies this A, the inside of the block will remain unchanged.

Int A = 8;

Int (^ myblock5) (int) = ^ (int)

{

Return A +;

};

A = 5;

Int result4 = myblock5 (3 );

NSLog (@ "result4 = % d", result4 );

// [Note] note that the copy value here is the value of a variable. If it is a memory location (address), that is to say, it is the pointer of this variable, its value is changed inside the block.

NSMutableArray * array = [[NSMutableArray alloc] initWithObjects: @ "one", @ "two", @ "three", nil];

Void (^ myblock6) (void) = ^ (void)

{

[Array removeLastObject];

};

// Insert the string @ "0" at 0th locations ";

[Array insertObject: @ "0" atIndex: 0];

Myblock6 ();

NSLog (@ "array = % @", array );

// Assign a value to sum. The sum value is changed.

Void (^ myblock7) (void) = ^ (void)

{

Sum = 6;

};

Myblock7 ();

NSLog (@ "sum = % d", sum );

 

// Static int B = 8;

// Int (^ myblock8) (int) = ^ (int)

//{

// Return B +;

//};

// B = 5;

// Int result5 = myblock8 (3 );

// NSLog (@ "result5 = % d", result5 );

Static int B = 8;

Int (^ myblock8) (int) = ^ (int)

{

B = 5;

Return B +;

};

Int result5 = myblock8 (3 );

NSLog (@ "result5 = % d", result5 );

// [Note] If you want to add a variable to the block operation, add a static modifier.

 

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.