Ios-delegate mode

Source: Internet
Author: User

The proxy pattern is, by definition, entrusted to others to do things.

There are two situations that are often encountered in iOS: The delegate mode in the Cocoa framework and the custom delegate mode. The following examples illustrate:

I. The delegate pattern in the Cocoa framework

In the delegate pattern in the Cocoa framework, the delegate is often the object in the frame (the control in the view, the table view of the Divine Horse), and the agent is often the view controller object.

In our case, UITableView is the principal, and the agent first has to satisfy a condition: it is stated in the . h file that it has the agent qualification:

@interface Whateverviewcontroller < uitableviewdelegate > @end

The red indicates that this view controller has the UITableView agent qualification.

Second, define in the . m file What the principal can do to replace the agent:

The view controller should have how many sections-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return [Nsarray count];}
How many rows should a view controller have on a section-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Sections { return [[Nsarray objectatindex:section]count];} View Controller to be responsible for the appearance of each column-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (NSINDEXPATH *) IND Expath {static NSString *cellidentifier = @ "Cell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuse Identifier:cellidentifier] autorelease]; } cell.textField.text = [Nsarray objectAtIndex:indexPath.row]; return cell;} Responsible for events that need to be triggered when the column is clicked-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath { Anotherviewcontroller *anotherviewcontroller = [[Anotherviewcontroller alloc] initwithnibname:@ "Anotherview" bundle:nil]; [Self.Navigationcontroller Pushviewcontroller:anotherviewcontroller]; [Anotherviewcontroller release];} This is optional, view controller diligent I will help you agent, not diligent I can not help you do this thing, (provide which lines can be edited)-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {return YES;}
operate on a specific editing style-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) editingst YLE Forrowatindexpath: (Nsindexpath *) Indexpath {if (Editingstyle = = Uitableviewcelleditingstyledelete) { [TableView Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] withrowanimation:yes]; } else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {}}
Optional, for specific operations on those moved columns-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Fromindexpath Toin Dexpath: (Nsindexpath *) Toindexpath {}
Returns yes-(BOOL) TableView for those rows that can be moved: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) Indexpath {//If you do not want the column to The cell moves, returns no return YES;

Well, when this client needs to do these things, the agent himself stands out to help. This is the delegate mode in iOS.

Second, the custom delegate mode

@interface A:uiviewid transparendvaluedelegate; @property (nomatic, retain) ID transparendvaluedelegate;@ End@implementation a@synthesize transparendvaluedelegate-(void) call{nsstring* value = @ "Hello"; [ Transparendvaluedelegate Transparendvalue:value];} @end

@interface b:uiviewnsstring* value; @end

@implementation B-(void) Transparendvalue: (nsstring*) fromvalue{value = Fromvalue; NSLog (@ "%@, I am B", value); } @end

When used:

* a = [[a alloc] init]; b* B = [[B alloc] init];a. transparendvaluedelegate = b;//Set a proxy delegate object to B[a call];

This will output:

Hello, I'm B.

The key to the entrustment model is a " by" Word. This b is very passive and will be called by you at any time.

Third, why there is delegate mode

In other words, can it be used to solve God's horse problem?

When certain functions of a class need to be implemented by others, but it is not clear what the function is, and it is unclear who will implement these functions, the delegation mode can come in handy.

For example, you can write a class C that implements-(void) Transparendvalue: (nsstring*) Fromvalue {NSLog (@ "%@, I am C", value);} It's also perfectly possible. I can entrust it to do this, as long as it implements this method.

Ultimately, it's all about making the coupling between classes more loosely. Good code should be open for extensions and closed for modifications .

Ios-delegate mode

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.