iOS协议
来源:互联网 发布:淘宝网上衣 编辑:程序博客网 时间:2024/06/13 17:29
iOS中协议虽然OC基础中有讲过,但一直只是表面的认识,今天在做项目时,有了更深入一些的认识。在请求网络数据并解析xml时协议就体现出它的价值了。解析数据后,最后会通过协议进行加载解析后的数据。
下面以例子说明:
示例一、
* 1、声明协议
@protocol updateSearchDataDelegate <NSObject>-(void)loadDataForShow;@end@interface AuctionDetailXmlParser : NSObject <NSXMLParserDelegate, ASIHTTPRequestDelegate>//定义协议@property (weak,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 (weak,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
0 0
- iOS 协议
- IOS协议
- IOS-协议
- iOS协议
- iOS-协议
- IOS 协议
- iOS协议
- IOS协议
- iOS 协议
- ios协议
- iOS网络协议--HTTP协议
- ios委托与协议
- iOS 自定义协议
- ios 正式协议protocol
- ios协议和委托
- ios正式协议
- IOS 自定义协议
- iOS 类目,延展,协议
- 基于bootstrap的autocomplete自动…
- 各类API文档下载
- 文章标题
- Codevs 1997 守卫者的挑战
- mysql日期函数小记01
- iOS协议
- C#原生连接数据库并操作数据库的方法
- 计蒜客挑战难题:泥塑课
- WEB-INF 访问权限说明
- c#三层架构
- java方法体的变量内存分配
- 时间戳改时间格式的方法
- 非常实用全面的 C++框架,库类等资源
- 2016TI杯——寻迹小车