自定义一个视图类似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;
}
然后再新建一个类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;
}
相关文章推荐
- 自定义一个类似uialertView的view
- 自定义ViewGroup(这里以自定义一个类似ViewPager的控件为例)
- iOS自定义提示弹出框实现类似UIAlertView的效果
- 源码推荐(10.30):完全自定义的提醒视图,随着键盘升起淡入一个view
- 初识kotlin实现一个类似烟花效果的自定义view
- 利用UIWindow自定义类似UIAlertView的弹出框
- 自定义UIAlertViewController选择视图
- 自定义一个类似listView和recylceView的需要setAdapter的控件
- 创建一个简单的表视图&自定义UITableView的表单元格
- 一个类似聊天气泡的自定义view 带个小箭头那种
- **Android三五行代码结局有滑块,可滑动的TABLE导航栏。类似斗鱼直播的直播页面导航栏,滑块可动态根据栏目宽度适应,无需自定义view** 先来说下思路把,很简单,一个HorizontalS
- Android上一个类似PathMenu效果的自定义View源码分析
- android自定义View学习(一)----创建一个视图类
- Android开发:创建自定义视图–创建一个View类
- iOS自定义提示弹出框实现类似UIAlertView的效果
- 添加自定义的View视图类(自定义一个进度条)
- 自定义一个全屏的view(subview)覆盖当前视图
- LyricView是一个强大而灵活的自定义视图,可以在Android的音乐播放器中显示歌词
- PPRevealSideViewController 一个类似Facebook iPhone客户端,可以左拉和右拉显示子视图
- Android开发之自定义View(视图)