好長時間沒寫東西了锨亏,這次先寫點簡單的
這次主要寫的是一個自定義的選擇器穷蛹,主要功能就是點擊按鈕彈出視圖控制器缤骨,然后根據(jù)選擇返回需要的值
首先定義一個視圖控制器
.h文件
#import <UIKit/UIKit.h>
@interface SelectViewController : UIViewController
@property (nonatomic, retain) UITableView *tableView;
@end
.m的實現(xiàn)
.m文件
#import "SelectViewController.h"
@interface SelectViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
{
NSMutableArray *_array;
}
@end
@implementation SubBrandViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
tapGR.delegate = self;
[self.view addGestureRecognizer:tapGR];
//需要彈出視圖的位置
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 100, 600, 590)];
[self.view addSubview: _tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorInset = UIEdgeInsetsZero;
_tableView.layer.cornerRadius = 10;
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
//創(chuàng)建需要的數(shù)據(jù)竟终,可以通過網(wǎng)絡(luò)請求獲取
_array = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
}
#pragma mark --UITableView delegate and datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId= @"SelectCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = _array[indexPath.row];
cell.textLabel.textColor = [UIColor colorWithRed:0.26f green:0.64f blue:0.84f alpha:1.00f];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[_array objectAtIndex:indexPath.row],@"Value",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SelectValue" object:nil userInfo:dict];
self.view.alpha = 0;
_tableView.alpha = 0;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _array.count;
}
-(void)dismiss{
self.view.alpha = 0;
_tableView.alpha = 0;
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 獲取手勢執(zhí)行的View
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(@"%@",NSStringFromClass([touch.view class]));
// 若為UITableViewCellContentView(即點擊了tableViewCell),則不截獲Touch事件
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}
#pragma mark -- Others
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
最后就是在需要得到值控制器中得到通知
//得到通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSelectValue:) name:@"SelectValue" object:nil];
//實現(xiàn)通知方法
- (void)setSubBrandValue:(NSNotification *)info{
//得到傳過來的值
NSString *string = [[info userInfo] objectForKey:@"Value"] ;
}