ios之protocol和delegate——下拉菜单实例

来源:互联网 发布:数据挖掘应用案例 编辑:程序博客网 时间:2024/05/06 20:36
  • DropDown是下拉菜单的实现,其中某一方法M,在不同情景下的实现不一样,它的具体实现要交给该控件的使用者A实现。
  • Delegate是两个类之间通信的一种实现方法。

DropDown.h中

1、 用@protocol声明代理DropDownDelegate

@protocol DropDownDelegate <NSObject>@optional//可选择实现的方法 ,@required是必须要实现的方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;//方法M@end

2、 添加代理DropDownDelegate

@interface DropDown : UIView <UITableViewDelegate,UITableViewDataSource,DropDownDelegate>

3、创建代理实例delegate

@property (nonatomic,assign) id<DropDownDelegate> delegate;

DropDown.m中

1、合成属性delegate

@synthesize delegate;

2、调用代理实现的方法部分

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{      ……//方法M的相同实现    [delegate tableView:tableView didSelectRowAtIndexPath:indexPath];//调用不同代理的对应实现}

A.h

1、添加协议DropDownDelegate

@interface A:UIViewController <DropDownDelegate>

2、创建DropDown实例

DropDown               *dropDown;

A.m

1、设置A为DropDown的代理

dropDown.delegate=self;

2、实现方法M

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString* dltString=[dltArr objectAtIndex:[indexPath row]];    int dlt=[dltString intValue];    NSLog(@"选中了%d",dlt);    ……//其他的具体实现}

具体实现部分

1.1、DropDown.h

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>//声明代理DropDownDelegate@protocol DropDownDelegate <NSObject>@optional- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;//方法M@end@interface DropDown : UIView <UITableViewDelegate,UITableViewDataSource,DropDownDelegate> {    UITableView *tv;//下拉列表    NSArray *tableArray;//下拉列表数据    UITextField *textField;    UIButton *btn;    BOOL showList;//是否弹出下拉列表    CGFloat tabheight;//table下拉列表的高度    CGFloat frameHeight;//frame的高度}@property (nonatomic,retain) UITableView *tv;@property (nonatomic,retain) NSArray *tableArray;@property (nonatomic,retain) UIButton *btn;@property (nonatomic,retain) UITextField *textField;@property (nonatomic,assign) id<DropDownDelegate> delegate;@end

1.2、DropDown.m

#import "DropDown.h"@implementation DropDown@synthesize tv,tableArray,textField,btn,delegate;-(id)initWithFrame:(CGRect)frame{    if (frame.size.height<200) {        frameHeight = 200;    }else{        frameHeight = frame.size.height;    }    tabheight = frameHeight-30;    frame.size.height = 30.0f;        self=[super initWithFrame:frame];        if(self){        showList = NO; //默认不显示下拉框                tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 0)];        tv.delegate = self;        tv.dataSource = self;        tv.backgroundColor = [UIColor grayColor];        tv.separatorColor = [UIColor lightGrayColor];        tv.hidden = YES;        [self addSubview:tv];                btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];        btn.frame=CGRectMake(0, 0, frame.size.width, 30);        [btn addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:btn];    }    return self;}-(void)dropdown{    if (showList) {//如果下拉框已显示,什么都不做        //        NSLog(@"下拉菜单已经显示");        return;    }else {//如果下拉框尚未显示,则进行显示        CGRect sf = self.frame;        sf.size.height = frameHeight;                //把dropdownList放到前面,防止下拉框被别的控件遮住        [self.superview bringSubviewToFront:self];        tv.hidden = NO;        showList = YES;//显示下拉框                CGRect frame = tv.frame;        frame.size.height = 0;        tv.frame = frame;        frame.size.height = tabheight;        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];        [UIView setAnimationCurve:UIViewAnimationCurveLinear];        self.frame = sf;        tv.frame = frame;        [UIView commitAnimations];    }}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [tableArray count];}- (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 = [tableArray objectAtIndex:[indexPath row]];    cell.textLabel.font = [UIFont systemFontOfSize:16.0f];    cell.accessoryType = UITableViewCellAccessoryNone;    cell.selectionStyle = UITableViewCellSelectionStyleGray;        return cell;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 35;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *selected=[tableArray objectAtIndex:[indexPath row]];    NSLog(@"选中了%@",selected);    [btn setTitle:selected forState:UIControlStateNormal];    showList = NO;    tv.hidden = YES;    CGRect sf = self.frame;    sf.size.height = 30;    self.frame = sf;    CGRect frame = tv.frame;    frame.size.height = 0;    tv.frame = frame;    [delegate tableView:tableView didSelectRowAtIndexPath:indexPath];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end

2.1、A.h

#include "DropDown.h"@interface A:UIViewController <DropDownDelegate>{    ……//其他变量    DropDown               *dropDown;}

2.2、A.m

    dropDown=[[DropDown alloc] initWithFrame:CGRectMake(x-97-gap, gap, 97, 100)];    dropDown.delegate=self;    [dropDown.btn setTitle:@"1" forState:UIControlStateNormal];    dropDown.tableArray = @{"1","2","3",nil};    [view addSubview:dropDown];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString* dltString=[dltArr objectAtIndex:[indexPath row]];    int dlt=[dltString intValue];    NSLog(@"选中了%d",dlt);}
0 0