您的位置:首页 > 产品设计 > UI/UE

自定义一个视图类似UIAlertView

2014-08-19 16:25 267 查看
先建立一个xib文件



然后再新建一个类CustomView继承自UIView

@protocol AddViewDeledate<NSObject>

- (void)cancel;

- (void)save;

@end
//红体部分是一个协议,有两个方法(void)cancel,(void)save

@interface CustomView : UIView

@property (strong, nonatomic) IBOutlet UITextField *myTextField;

@property (assign, nonatomic) id<AddViewDeledate> delegate; //这是一个代理,哪个对象实现了 <AddViewDeledate>协议,就可以使是这个自定义视图的代理

- (IBAction)PressCancel:(id)sender;

- (IBAction)PressOk:(id)sender;

@end

- (IBAction)PressCancel:(id)sender {

if([self.delegate respondsToSelector:@selector(cancel)])

{

[self.delegate cancel]; //因为是这个视图的代理,就肯定实现了协议方法,就可以调用这个已经实现的协议方法,这一点非常重要,因为通过代理,可以实现两个视图之间传送消息

}

}

- (IBAction)PressOk:(id)sender {

if ([self.delegate respondsToSelector:@selector(save) ]) {

[self.delegate save]; //因为是这个视图的代理,就肯定实现了协议方法,就可以调用这个已经实现的协议方法

}

}

//使用,比如必要使用自定义视图,你必须实现这个协议,再实现代理方法

#import "CustomView.h"

@interface YcwFirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,AddViewDeledate>

@property (strong, nonatomic) IBOutlet UITableView *myTableView;

@end

#import "YcwFirstViewController.h"

#import "CustomView.h"

@interface YcwFirstViewController ()

{

NSMutableArray *mArray;

CustomView *customView;

}

@end

- (void)viewDidLoad

{

[super viewDidLoad];

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addView:)];

self.navigationItem.leftBarButtonItem = leftButton;

self.myTableView.delegate = self;

self.myTableView.dataSource = self;

mArray = [NSMutableArray arrayWithObjects:@"nike",@"adidas",@"vans", nil];

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)addView:(id)sender

{

NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil];

customView = nib[0];

customView.delegate = self;
//这句话非常重要

[customView.myTextField becomeFirstResponder];

customView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.4];

[self.view addSubview:customView];

}

- (void)save
// 实现协议方法

{

if (customView !=nil) {

NSString *brand = customView.myTextField.text;

[mArray addObject:brand];

[self.myTableView reloadData];

[customView removeFromSuperview];

}

}

- (void)cancel // 实现协议方法

{

if (customView !=nil) {

[customView removeFromSuperview];

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [mArray count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *cellIden = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];

if (cell ==nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden];

}

cell.textLabel.text = [mArray objectAtIndex:indexPath.row];

return cell;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: