一、协议
ios中协议虽object-c基础中有讲过,但一直只是表面的认识,今天在做项目时,有了深些的认识。在请求网络数据并解析xml时协议就体现出它的价值了。解析数据后,最后会通过协议进行加载解析后的数据。
下面以例子说明:
示例一、
1、声明协议
@protocol updateSearchDataDelegate <NSObject>
-(void)loadDataForShow;
@end
@interface AuctionDetailXmlParser : NSObject<NSXMLParserDelegate,ASIHTTPRequestDelegate>
//定义协议
@property (strong,nonatomic) id<updateSearchDataDelegate> delegate;
........
@end
可以用协议delegate调用loadDataForShow方法。
2、当其它类引入协议时,则需要实现协议里面的方法。
@interface AuctionDetailsViewController : UITableViewController<updateSearchDataDelegate>
......
@end
@implementation AuctionDetailsViewController
- (void)getAuctionDetails{
auctionDetailXmpParser = [[AuctionDetailXmlParser alloc] init];
//设置代理,只有设置协议才会调用协议中方法
auctionDetailXmpParser.delegate = self;
[auctionDetailXmpParser getAuctionDetails:strXml Action:actionName];
}
- (void)loadDataForShow{
........
}
@end
示例二(当请求另一个viewController时,通过协议可不通过new来请求该viewController方法 ):
#import <UIKit/UIKit.h> #import "RequestVO.h" //协议 @protocol sortDataDelegate<NSObject> @required - (void) loadDataShow : (RequestVO *)requestParams; @end @interface SortViewController : UITableViewController @property (strong,nonatomic) id<sortDataDelegate> delegate; @end
// // SortViewController.m // BookReader // // Created by Dwen on 13-1-29. // // #import "SortViewController.h" #import "Utils.h" @interface SortViewController () @end @implementation SortViewController @synthesize popover,sortArry; @synthesize sortStr; @synthesize specialCode; @synthesize delegate; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { sortArry = [[NSMutableArray alloc] initWithObjects:CLOSE_COST_ASC,CLOSE_COST_DESC,LOT_ASC,LOT_DESC, nil]; } return self; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text =@"test"; cell.textLabel.font = [UIFont fontWithName:@"Arial" size:18]; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { RequestVO *requestVO = [[RequestVO alloc] init]; [delegate loadDataShow:requestVO];//****代理方法解决调用Controller问题 } @end
#import <UIKit/UIKit.h> @interface CatalogViewController : UIViewController<sortDataDelegate> @end @interface CatalogViewController () @end @implementation CatalogViewController //排序 - (IBAction)sortAction:(id)sender { sortVC = [[SortViewController alloc] initWithStyle:UITableViewStylePlain]; sortVC.contentSizeForViewInPopover = CGSizeMake(240, 176); popover = [[UIPopoverController alloc] initWithContentViewController:sortVC]; [popover presentPopoverFromRect:[self.sortBtn bounds] inView:self.sortBtn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; sortVC.delegate = self;//****该delegate一定要设 } - (void)loadDataShow:(RequestVO *)requestParams{ } @end
二、分类
分类是对类的扩展,当类较大时,想扩展新的方法时,可采用分类进行处理。分类中不能定义变量,只能定义方法。分类可以访问原始类的变量和方法。分类命名规则以+分隔
示例代码:
#import <Foundation/Foundation.h> @interface Test : NSObject @property (strong , nonatomic) NSString *a; - (void) test; @end
#import "Test.h" @interface Test () @end @implementation Test @synthesize a; - (void) test{ a = @"a变量"; NSLog(@"test........."); }
Test+TestCategory.h
#import "Test.h" @interface Test (TestCategory) - (void) testCategory; @end
Test+TestCategory.m
#import "Test+TestCategory.h" @implementation Test (TestCategory) - (void) testCategory{ [self test]; NSLog(@"testCategory........%@",self.a); } @end
main方法调用:
#import <UIKit/UIKit.h> #import "AppDelegate.h" #import "Test+TestCategory.h" int main(int argc, char *argv[]) { Test *test = [[Test alloc] init]; [test test]; [test testCategory]; @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } //输出结果 //2012-10-18 18:23:03.377 Test102[33019:f803] test......... //2012-10-18 18:23:03.381 Test102[33019:f803] test......... //2012-10-18 18:23:03.390 Test102[33019:f803] testCategory........a变量
相关推荐
IOS 商家分类demo
这是一篇介绍IOS多线程理解的文章,简洁易懂,方便阅读
iOS 各种分类 集合
简单的关于iOS协议和代理的例子,适合新手参考,改变界面的颜色。
用代码清晰的解释了协议的用法与使用场景,以及各个类之间用协议protocol通信的规则。欢迎来学习,同时希望提出意见共同进步
ios中非常重要的delegate和protocol的通俗讲解,非常清晰易懂,看了之后,让你对代理和协议有不一样的认识。
iOS常见分类 欢迎大家下载学习
ios中的协议,是我们开发中经常用到的开发模式。我这里只是最基础的东西。。希望对初学的伙伴们用得着
iOS常用的Category分类你值得拥有。iOS常用的Category分类你值得拥有。
ios demo,协议的定义和实现,ios demo,协议的定义和实现。
IOS 各层协议关系图,对于初学者很有帮助
对于IOS的代理模式,协议什么的,看说明是很混淆的,看代码就明了多了
关于HTTP get post请求,HTTP协议的含义,以及在 IOS里,HTTP协议怎么用
ios 协议传值模版,demo,新手一看就会,代码有注释
ios快速开发的分类,ios纯代码布局,直接用的view的各个属性,ios常用控件的分类,方便快捷集成,包括UILable、UIButton、UIView左边的计算等等。
ios分类方法大全,包含各种UI控件、常用类方法
ios协议传值
利用分类无侵入性的实现 从左侧划入的侧边栏
ios mdm 管理协议
选择分类,电商选择分类