天天看点

iOS 心得六 地区时举器

日了孝天犬了,刚刚看见我居然没有写心得六直接写了七,这不能容忍。所以我精心挑选一个很常使用的iOS小功能作为心得六的知识点(可以直接拷贝进去使用的demo)。经过几次构思,决定把地区时举器写一个。因为基本上做app都会有地址这一选项。而苹果手机90%都是用时举器去显示地址。现在进入正题。

首先,和往常一样,demo放在github上,地址:https://github.com/sunyunfei/AreaDemo.git

时举器  UIPickerView 是一个我不是经常用的控件。但是地址选择,日期显示基本上还是选择他的,不为别的,他省空间,显示形式比较好。它和表思路基本是一样的,两个代理UIPickerViewDelegate,UIPickerViewDataSource。我再说这个demo的基础是你已经学习过时举器,如果你还没有,那么请你看一下他的相关的资料。

首先我创建一个UIPickerView的视图。

.h

#import <UIKit/UIKit.h>

#import "AreaModel.h"

@interface AreaPickerView : UIView<UIPickerViewDelegate, UIPickerViewDataSource>

//取消按钮事件

- (IBAction)clickCancelBtn:(UIBarButtonItem *)sender;

//确定按钮事件

- (IBAction)clickOkBtn:(UIBarButtonItem *)sender;

//时举器

@property (weak, nonatomic) IBOutlet UIPickerView *areaPicker;

@property (strong, nonatomic) AreaModel *locate;

//选中块

@property(nonatomic,copy)void(^chooseBlock)(AreaPickerView *area);

//取消块

@property(nonatomic,copy)void(^cancelBlock)();

//确定块

@property(nonatomic,copy)void(^okBlock)();

- (id)initWithArea;

- (void)showInView:(UIView *)view;

- (void)cancelPicker;

@end

.m

#import "AreaPickerView.h"

@interface AreaPickerView()

{

     NSArray *provinces, *cities, *areas;

}

@end

@implementation AreaPickerView

- (id)initWithArea

{

    self = [[[NSBundle mainBundle] loadNibNamed:@"AreaPickerView" owner:self options:nil] objectAtIndex:0];

    if (self) {

        self.areaPicker.dataSource = self;

        self.areaPicker.delegate = self;

        //加载数据

            provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];

            cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];

            self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"state"];

            self.locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];

            areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

            if (areas.count > 0) {

                self.locate.district = [areas objectAtIndex:0];

            } else{

                self.locate.district = @"";

            }

    }

    return self;

}

-(AreaModel *)locate

{

    if (_locate == nil) {

        _locate = [[AreaModel alloc] init];

    }

    return _locate;

}

#pragma mark - PickerView lifecycle

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 3;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    switch (component) {

        case 0:

            return [provinces count];

            break;

        case 1:

            return [cities count];

            break;

        case 2:

                return [areas count];

                break;

        default:

            return 0;

            break;

    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

        switch (component) {

            case 0:

                return [[provinces objectAtIndex:row] objectForKey:@"state"];

                break;

            case 1:

                return [[cities objectAtIndex:row] objectForKey:@"city"];

                break;

            case 2:

                if ([areas count] > 0) {

                    return [areas objectAtIndex:row];

                    break;

                }

            default:

                return  @"";

                break;

        }

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

        switch (component) {

            case 0:

                cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];

                [self.areaPicker selectRow:0 inComponent:1 animated:YES];

                [self.areaPicker reloadComponent:1];

                areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

                [self.areaPicker selectRow:0 inComponent:2 animated:YES];

                [self.areaPicker reloadComponent:2];

                self.locate.state = [[provinces objectAtIndex:row] objectForKey:@"state"];

                self.locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];

                if ([areas count] > 0) {

                    self.locate.district = [areas objectAtIndex:0];

                } else{

                    self.locate.district = @"";

                }

                break;

            case 1:

                areas = [[cities objectAtIndex:row] objectForKey:@"areas"];

                [self.areaPicker selectRow:0 inComponent:2 animated:YES];

                [self.areaPicker reloadComponent:2];

                self.locate.city = [[cities objectAtIndex:row] objectForKey:@"city"];

                if ([areas count] > 0) {

                    self.locate.district = [areas objectAtIndex:0];

                } else{

                    self.locate.district = @"";

                }

                break;

            case 2:

                if ([areas count] > 0) {

                    self.locate.district = [areas objectAtIndex:row];

                } else{

                    self.locate.district = @"";

                }

                break;

            default:

                break;

        }

    //选中块

    self.chooseBlock(self);

}

- (void)showInView:(UIView *)view

{

    CGFloat width = [UIScreen mainScreen].applicationFrame.size.width;

    CGFloat height = [UIScreen mainScreen].applicationFrame.size.height;

    self.frame = CGRectMake(0, height, width, self.frame.size.height);

    [view addSubview:self];

    [UIView animateWithDuration:0.3 animations:^{

        self.frame = CGRectMake(0, height - self.frame.size.height, width, self.frame.size.height);

    }];

}

- (void)cancelPicker

{

    [UIView animateWithDuration:0.3

                     animations:^{

                         self.frame = CGRectMake(0, self.frame.origin.y+self.frame.size.height, self.frame.size.width, self.frame.size.height);

                     }

                     completion:^(BOOL finished){

                         [self removeFromSuperview];

                     }];

}

- (IBAction)clickCancelBtn:(UIBarButtonItem *)sender {

    self.cancelBlock();

}

- (IBAction)clickOkBtn:(UIBarButtonItem *)sender {

    self.okBlock();

}

@end

其实你浏览一下就知道使用和表一样的,确定行数确定列数,选定方法之类的。我这是自己用nib画了一个时举器

iOS 心得六 地区时举器

然后再控制器创建一个按钮做为调出时举器的按钮, .h

#import "ViewController.h"

#import "AreaPickerView.h"

@interface ViewController ()

- (IBAction)clickBtn:(UIButton *)sender;

//地址按钮

@property (weak, nonatomic) IBOutlet UIButton *areaBtn;

@property (strong, nonatomic) NSString *areaValue;

@property (strong, nonatomic) AreaPickerView *locatePicker;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self cancelLocatePicker];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

- (IBAction)clickBtn:(UIButton *)sender {

    self.locatePicker = [[AreaPickerView alloc]

                         initWithArea];

    //块的相关操作

    __weak typeof(self)weakSelf = self;

    self.locatePicker.chooseBlock= ^(AreaPickerView *area)

    {

        weakSelf.areaValue = [NSString stringWithFormat:@"%@ %@ %@", area.locate.state, area.locate.city, area.locate.district];

    };

    self.locatePicker.cancelBlock = ^()

    {

        [weakSelf cancelLocatePicker];

    };

    self.locatePicker.okBlock = ^()

    {

        if( (weakSelf.areaValue == nil) || [weakSelf.areaValue isEqualToString:@"北京 北京 通州"])

        {

            weakSelf.areaValue = @"北京 北京 通州";

        }

        [weakSelf.areaBtn

                        setTitle:weakSelf.areaValue

                        forState:UIControlStateNormal];

        [weakSelf cancelLocatePicker];

    };

    [self.locatePicker showInView:self.view];

}

#pragma mark - HZAreaPicker delegate

-(void)pickerDidChaneStatus:(AreaPickerView *)picker

{

    self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];

    NSLog(@"%@",self.areaValue);

}

-(void)cancelLocatePicker

{

    [self.locatePicker cancelPicker];

    self.locatePicker = nil;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];

    if( (self.areaValue == nil) || [self.areaValue isEqualToString:@"北京 北京 通州"])

    {

        self.areaValue = @"北京 北京 通州";

    }

    [self.areaBtn

                setTitle:self.areaValue

                forState:UIControlStateNormal];

    [self cancelLocatePicker];

}

@end

这样,一个地区时举器就完成了

iOS 心得六 地区时举器

在这里面我是留了一个小bug的,不知道你们能不能看出来。看出来自己修改一下,相信自己的能力,很快就可以成功。