簡(jiǎn)單實(shí)用的選擇器

好長(zhǎng)時(shí)間沒寫東西了,這次先寫點(diǎn)簡(jiǎn)單的
這次主要寫的是一個(gè)自定義的選擇器,主要功能就是點(diǎn)擊按鈕彈出視圖控制器,然后根據(jù)選擇返回需要的值

首先定義一個(gè)視圖控制器

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

@interface SelectViewController : UIViewController

@property (nonatomic, retain) UITableView *tableView;

@end

.m的實(shí)現(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ò)請(qǐng)求獲取
    _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 -- 獲取手勢(shì)執(zhí)行的View
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    NSLog(@"%@",NSStringFromClass([touch.view class]));
    // 若為UITableViewCellContentView(即點(diǎn)擊了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];

//實(shí)現(xiàn)通知方法
- (void)setSubBrandValue:(NSNotification *)info{
        //得到傳過來的值
       NSString *string = [[info userInfo] objectForKey:@"Value"] ;
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容