IOS study notes I understand the OC-proxy mode, 2015-03-27oc-

Source: Internet
Author: User

IOS study notes I understand the OC-proxy mode, 2015-03-27oc-

Case 1

KCButton. h //// KCButton. h // Protocol & Block & Category /// Created by Kenshin Cui on 14-2-2. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # import <Foundation/Foundation. h> @ class KCButton; // one protocol can be used to expand another protocol, for example, KCButtonDelegate extends the NSObject protocol @ protocol KCButtonDelegate <NSObject> @ required // The @ required modifier must be implemented-(void) onClick :( KCButton *) button; @ optional // @ optional the modifier is optional-(void) onMouseover :( KCButton *) button;-(void) onMouseout :( KCButton *) button; @ end @ interface KCButton: NSObject # pragma mark-attribute # pragma mark proxy attribute. It is also agreed that the object as a proxy must implement the KCButtonDelegate Protocol @ property (nonatomic, retain) id <KCButtonDelegate> delegate; # pragma mark-common method # pragma mark click method-(void) click; @ end
KCButton. m //// KCButton. m // Protocol & Block & Category /// Created by Kenshin Cui on 14-2-2. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # import "KCButton. h "@ implementation KCButton-(void) click {NSLog (@" Invoke KCButton's click method. "); // determine whether the _ delegate instance has implemented onClick: method (note that the method name is" onClick :", there will be a later class :) // avoid failing to implement ButtonDelegate as the KCButton listening if ([_ delegate respondsToSelector: @ selector (onClick :)]) {[_ delegate onClick: self] ;}@ endMyListener. h //// MyListener. h // Protocol & Block & Category /// Created by Kenshin Cui on 14-2-2. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # import <Foundation/Foundation. h> @ class KCButton; @ protocol KCButtonDelegate; @ interface MyListener: NSObject <KCButtonDelegate>-(void) onClick :( KCButton *) button; @ endMyListener. m /// MyListener. m // Protocol & Block & Category /// Created by Kenshin Cui on 14-2-2. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # import "MyListener. h "# import" KCButton. h "@ implementation MyListener-(void) onClick :( KCButton *) button {NSLog (@" Invoke MyListener's onClick method. the button is: % @. ", button) ;}@ endmain. m /// main. m // Protocol & Block & Category /// Created by Kenshin Cui on 14-2-2. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # import <Foundation/Foundation. h> # import "KCButton. h "# import" MyListener. h "int main (int argc, const char * argv []) {@ autoreleasepool {KCButton * button = [[KCButton alloc] init]; myListener * listener = [[MyListener alloc] init]; button. delegate = listener; [button click];/* result: Invoke KCButton's click method. invoke MyListener's onClick method. the button is: <KCButton: 0x1001034c0>. */} return 0 ;}
We use an example to simulate the button clicking process, which is somewhat similar to the event implementation mechanism in Java. In this example, we need to pay attention to the following points: id can represent any ObjC object type, the "<protocol name>" after the type is used to constrain the objects used as this attribute must implement this protocol (Note: The object type defined by id does not need to be added with "*"); as the trigger of an event, MyListener implements the KCButtonDelegate proxy (there is no namespace or package concept in ObjC, and classes are usually divided by prefixes, "KC" is our custom prefix) in. if the class or protocol of another file is used in the H file, we can declare it through @ class or @ protocol without importing the file, this improves compilation efficiency (note that @ class or @ protocol must be used in some cases, for example, KCButton. in h, the KCButtonDelegate Protocol stated above uses the KCButton class, And the KCButton class declaration below this file uses KCButtonDelegate to form a reference relationship between each other in a file, you must use @ class or @ Protocol declaration, otherwise an error will be reported in the compilation phase), but in. in the m file, you must import the corresponding class declaration file or protocol file (if you do not import the file, although the syntax check can be passed, but the compilation link will report an error ); the respondsToSelector method can be used to determine whether an object has implemented a certain method (the method name is not "onClick" but "onClick:", and the colon is also a part of the method name); In the attribute (nonatomic, retain) is not the focus of this Article. We will introduce it in the next article.

 

Case 2

/// WPContactsViewController. h // OC-UI-form-cell /// Created by wangtouwang on 15/3/26. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <UIKit/UIKit. h> @ interface WPContactsViewController: UIViewController {UITableView * _ tableView; // form UI control NSMutableArray * _ mutableArrayContacts; // contact set model NSIndexPath * _ selectedIndexPath; // The selected group and row} @ end
/// WPContactsViewController. m // OC-UI-form-cell /// Created by wangtouwang on 15/3/26. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import "WPContactsViewController. h "# import" WPContacts. h "# import" WPContactsGroup. h "@ interface WPContactsViewController () <UITableViewDataSource, metrics, metrics> @ end @ implementation WPContactsViewController-(void) viewDidLoad {[super viewDidLoad]; // initialize data [self initObjectData]; // create a group style UITableView _ tableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStyleGrouped]; // set the data source. Note that the corresponding UITableViewDataSource Protocol _ tableView must be implemented. dataSource = self; // sets proxy _ tableView. delegate = self; [self. view addSubview: _ tableView];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} # pragma mark-(void) initObjectData {_ mutableArrayContacts = [[NSMutableArray alloc] init]; WPContacts * contact1 = [WPContacts metadata: @ "Cui" andLastName: @ "Kenshin" andPhoneNumber: @ "18500131234"]; WPContacts * contact2 = [WPContacts initStaticWithFirstName: @ "Cui" andLastName: @ "Tom" andPhoneNumber: @ "18500131237"]; WPContactsGroup * group1 = [WPContactsGroup succeeded: @ "C" andDescript: @ "With names beginning with C" andConcats: [NSMutableArray succeeded: contact1, contact2, nil]; [_ mutableArrayContacts addObject: group1]; WPContacts * contact3 = [WPContacts initStaticWithFirstName: @ "Dui" andLastName: @ "JACK" andPhoneNumber: @ "13712133321"]; WPContacts * contact4 = [WPContacts metadata: @ "Dui" andLastName: @ "LUCY" andPhoneNumber: @ "13712133322"]; WPContactsGroup * group2 = [WPContactsGroup metadata: @ "D" andDescript: @ "With names beginning with D" andConcats: [NSMutableArray arrayWithObjects: contact3, contact4, nil]; [_ mutableArrayContacts addObject: group2];} # pragma mark returned group count-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {NSLog (@ "number of groups"); return _ mutableArrayContacts. count ;}# pragma mark returns the number of rows in each group-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {NSLog (@ "calculate each group (group % lu) number of rows ", (unsigned long) section); // WPContactsGroup * group1 = _ mutableArrayContacts [section]; return (WPContactsGroup *) _ mutableArrayContacts [section]). _ concats. count ;}# pragma mark returns the cells in each row-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "generate cells (group: % lu, row % lu) ", (unsigned long) indexPath. section, (unsigned long) indexPath. row); WPContactsGroup * group = _ mutableArrayContacts [indexPath. section]; WPContacts * contacts = group. _ concats [indexPath. row]; UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: nil]; cell. textLabel. text = [contacts getName]; cell. detailTextLabel. text = [contacts _ phoneNumber]; return cell ;}# pragma mark returns the name of each group of headers-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {NSLog (@ "generate group (group % lu) Name", (unsigned long) section); WPContactsGroup * group = _ mutableArrayContacts [section]; return group. _ name ;}# pragma mark returns the description of the end of each Group-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section {NSLog (@ "generate tail (group % lu) details ", (unsigned long) section); return (WPContactsGroup *) _ mutableArrayContacts [section]). _ descript ;}# index generated by pragma mark-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {NSLog (@ "generate Group Index "); NSMutableArray * indexs = [[NSMutableArray alloc] init]; for (WPContactsGroup * group in _ mutableArrayContacts) {[indexs addObject: group. _ name];} return indexs;} # pragma mark sets the height of the group title-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section {NSLog (@ "set group title height"); return 25 ;}# pragma mark sets the content height at the end of the Group-(CGFloat) tableView :( UITableView *) tableView heightForFooterInSection :( NSInteger) section {NSLog (@ "set the content height at the end of the group"); return 20 ;}# pragma mark sets the height of each row-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "set the height of each row"); return 30 ;}# pragma mark rewrite setting click-row triggering event (the rewrite method is that it is already in the TableViewDelegate Protocol and called back) -(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "Trigger me"); _ selectedIndexPath = indexPath; // obtain the WPContacts * contacts = (WPContactsGroup *) _ mutableArrayContacts [indexPath. section]). _ concats [indexPath. row]; // initialization pop-up box UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "System Info" message: [contacts getName] delegate: self cancelButtonTitle: @ "Cancle" otherButtonTitles: @ "OK", nil]; // sets the window style alertView. alertViewStyle = UIAlertViewStylePlainTextInput; // obtain the text box UITextField * textFild = [alertView textFieldAtIndex: 0]; // set the text box content textFild. text = contacts. _ phoneNumber; // display window [alertView show];} # pragma mark settings pop-up box subsequent button event replay UIAlerViewDelegate protocol click button clickedButtonAtIndex method-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {NSLog (@ "set the event of the subsequent button in the pop-up box clickedButtonAtIndex"); if (buttonIndex = 1) {// obtain the content of the text box NSString * phoneNumber = [alertView textFieldAtIndex: 0]. text; // obtain the WPContacts * contacts = (WPContactsGroup *) _ mutableArrayContacts [_ selectedIndexPath. section]). _ concats [_ selectedIndexPath. row]; contacts. _ phoneNumber = phoneNumber; NSArray * indexPaths = @ [_ selectedIndexPath]; // The group and row of cells to be partially refreshed. // This optimization changes the global update to the selected row. [_ tableView reloadRowsAtIndexPaths: indexPaths withRowAnimation: UITableViewRowAnimationLeft]; // [_ tableView reloadData] ;}}@ end

 

 

 

 

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.