Cocoa design model Delegation

Source: Internet
Author: User

Protocol-the protocol is used to follow the protocol, and the implementation method required by the protocol must be implemented.

Delegate-delegation, as its name implies, is to entrust others to handle affairs, that is, when one thing happens, it will not be handled by others.

If A view contains B view and B view needs to be modified on the view Interface, delegation is required.

Several steps are required

1. First define an agreement

2. Methods of a view Implementation Protocol

3. B view sets a delegate variable.

4. Set the delegate variable of B view to a view, which means that B view delegates a view to do things.

5. After an event occurs, use the delegate variable to call the Protocol method in a view.

The steps below are described as follows:

1. First define a protocol

@protocol PlayerDetailDelegate <NSObject>-(void) updatePlayer:(Player *)player;@end

This is a formal way to define the protocol, and also an informal Protocol. Its essence is Category, which extends the existing class methods.

2. Methods of a view Implementation Protocol

First, add the implementation protocol to the header file.

@interface PlayersViewController : UITableViewController<PlayerDetailDelegate>{    Player *selectedPlayer;}@property(nonatomic,strong) NSMutableArray *players;@end

Then implement the methods in the Protocol in the implementation file

-(void) updatePlayer:(Player *)player{    selectedPlayer = player;    NSLog(@"name%@",selectedPlayer.name);}

3. Set a delegate variable in B view.

@interface PlayerDetailViewController : UIViewController{    id<PlayerDetailDelegate> _playerDelegate;}@property(nonatomic,strong) IBOutlet UITextView *detailView;@property(nonatomic,strong) Player *player;@property(nonatomic,strong) id<PlayerDetailDelegate> _playerDelegate;- (IBAction)buttonPressed:(id)sender;@end

Call the delegate object in a proper place

-(IBAction) buttonPressed :( id) sender {if (_ playerDelegate! = Nil & [_ playerDelegate respondsToSelector: @ selector (updatePlayer :)]) {self. player. name = self. detailView. text; [_ playerDelegate updatePlayer: self. player];} NSLog (@ "Run! ");}

4. Set the delegate variable of B view to a view, which means that B view delegates a view to do things.

5. After an event occurs, use the delegate variable to call the Protocol method in a view.

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.