简单实用的选择器

2016-10-19  本文已影响35人  ly渐行渐远

好长时间没写东西了,这次先写点简单的
这次主要写的是一个自定义的选择器,主要功能就是点击按钮弹出视图控制器,然后根据选择返回需要的值

首先定义一个视图控制器

.h文件
#import <UIKit/UIKit.h>

@interface SelectViewController : UIViewController

@property (nonatomic, retain) UITableView *tableView;

@end

.m的实现

.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];
    
//创建需要的数据,可以通过网络请求获取
    _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 -- 获取手势执行的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];

//实现通知方法
- (void)setSubBrandValue:(NSNotification *)info{
        //得到传过来的值
       NSString *string = [[info userInfo] objectForKey:@"Value"] ;
  }
上一篇 下一篇

猜你喜欢

热点阅读