IOS sharing and ios sharing

Source: Internet
Author: User
Tags notification center

IOS sharing and ios sharing

In project development, communication between objects is often involved. To reduce the coupling between objects, delegate, icationcenter center, and block methods are used for implementation, you may be familiar with it, but it may be just a matter of understanding for beginners how to create it. In this article, I will not talk about it for a long time. I will only use simple examples to help you learn how to use it, I hope to help those who do not know the above three.

 1. delegate

It is estimated that the most common delegate is UITableViewDelegate. How can we write a proxy by ourselves? We often listen to button clicks in a view in the Controller. This is an example. If there is a Button in DTestView, we need to listen to the Button click in DTestViewController and pass an array to DTestViewController when the Button is clicked. The Code is as follows:

// 1. In DTestView. declare the protocol in the hfile and create the protocol method @ class DTestView; @ protocol DTestViewDelegate <NSObject> @ optional/** click the button and pass the array information */-(void) dTestViewDidClickBtn :( DTestView *) dTestView withArr :( NSArray *) position; @ end // 2. declare the delegate attribute in the H file
@ Interface DTestView: UIView
@ Property (nonatomic, weak) id <DTestViewDelegate> delegate; @ end

// 3. In DTestView. in the m file, call the delegate protocol method in the Button click event-(void) btnClick {if ([self. delegate respondsToSelector: @ selector (dTestViewDidClickBtn: withArr :)]) {NSArray * arr = @ ["test", "delegate"]; [self. delegate dTestViewDidClickBtn: self withArr: arr] ;}}

There are three steps to create a proxy. How can this problem be solved? However, there are several points to note:

1. Naming rules: class name + Delegate.

2. @ optional Keyword: after the Protocol is followed, the Protocol method can not be implemented. The default Protocol method is @ required, that is, the method must be implemented after the Protocol is followed, we recommend that you use @ optional to modify methods that are not required.

3. In the ARC environment, the delegate attribute is modified with weak.

4. Before calling the proxy method, use the respondsToSelector: method to determine whether the proxy method exists.

The above are the notes for creating delegate. You can think about why you should do this. If you do not understand anything, please ask questions. If there is anything wrong, please correct me.

The next step is the use of delegate. To put it simply, it is also three steps:

// 1. Set the proxy-(void) viewDidLoad {[super viewDidLoad]; dTestView * view = [[dTestView alloc] init]; view. delegate = self; [self. view addSubview: view];} // 2. Follow the Protocol @ interface DTestViewController () <DTestViewDelegate> // 3. Implement the Protocol method-(void) dTestViewDidClickBtn :( DtestView *) dTestView withArr :( NSArray *) arr {NSLog (@ "% @", arr );}

Well, this is the creation and use of a simple delegate.

2. icationicationcenter

The notification center is relatively simple. The following example uses the listener button to click and pass an array to publish a notification in NTestView:

// Publish a notification in the addTarget Click Event of the Button-(void) btnClick {NSArray * arr = @ [@ "test", @ "icationicationcenter"]; [[nsicationcenter center defacenter center] postNotificationName: @ "click" object: nil userInfo: arr];}

Finished? Now, it's so easy. Next, add the observer in NTestViewController:

// Add an observer-(void) viewDidLoad {[super viewDidLoad]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (nTestViewDidClickBtn :) name: @ "click" object: nil];}/** call this method when a notification is received */-(void) nTestViewDidClickBtn :( NSNotification *) note {NSArray * arr = note. userInfo; NSLog (@ "% @", arr);} // remove the observer-(void) dealloc {[[nsicationicationcenter defacenter center] removeObserver: self];}

The following parameters are explained:

Name: Notification name

Object: the object to be passed

UserInfo: the information transmitted when a notification is published (this parameter can be received by default when an observer is added)

The above is the simple use of icationicationcenter.Note: When you do not need to listen, you must remove the observer..

3. block (closure)

 The simple use of block still uses the listener button to click and pass an array as an example:

// 1. In BtestView. in the H file, we can provide such a class method for initialization: @ interface BTestView: UIView/** block attribute */@ property (nonatomic, copy) void (^ arrBlock) (NSArray *);/** Initialization Method */+ (instancetype) bTestViewWithArrBlock :( void (^) (NSArray * arr) arrBlock; @ end // 2, in BTestView. in the m file, implement this method: + (instancetype) bTestViewWithaArrBlock :( void (^) (NSArray * arr) arrBlock {BTestView * view = [[BTestView alloc] init]; _ arrBlock = arrBlock; return view;} // 3. Call arrBlock-(void) clickBtn {NSArray * arr = @ [@ "test ", @ "icationicationcenter"]; _ arrBlock (arr );}

Use block in NTestViewController:

- (void)viewDidLoad {   [super viewDidLoad];   BTestView *view = [BTestView bTestViewWithArrBlock:^(NSArray *arr){    NSLog(@"%@", arr);  }];   [self.view addSubview:view]; }

I don't know if you are familiar with block usage?

  Note: The block attribute must be modified with copy. When using the block attribute, be sure not to cause circular references.

Which of the three communication modes are used? This can only be selected based on the actual project. A common saying is: One-to-multiple-channel credit icationcenter center, one-to-one communication uses blocks when there are few methods, and delegate is used when there are many methods.

  Note: The above code has not passed the actual test. This article only provides a simple idea for use.

 

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.