iOS学习-3

2020-01-27  本文已影响0人  Ello_Orld

1. 日期选择器

UIDataPicker 日期选择器
有四种模式:

  • 日期
  • 日期时间
  • 时间
  • 倒计时

打开故事板文件,拖拽datepicker、label和button到响应的位置上。

image.png

看一下datePicker属性:


image.png
//
//  ViewController.m
//  test0127
//
//  Created by dxl on 2020/1/27.
//  Copyright © 2020 dxl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)buttonClicked:(id)sender {
    NSDate *date = self.datePicker.date;
    NSLog(@"current date = %@", [date descriptionWithLocale:[NSLocale currentLocale]]);
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
    dateFormater.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    self.dateLabel.text = [dateFormater stringFromDate:date];
}

@end

2. 普通选择器UIPickerView

实现省份城市的联动
首先在故事板中添加一个pickerView,一个label和一个button


image.png

pickerView不需要设置任何属性

右键pickerView控件,将DataSource和delegate拖到布局中ViewController目录上。

到代码中:
添加数据源 provinces_cities.plist文件到项目中

读取数据:

NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"provinces_cities" ofType:@"plist"];
_pickerData = [[NSDictionary alloc]initWithContentsOfFile:plistPath];

实现代理和数据源

@interface ViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>

实现数据源对应的方法
完整代码如下:

//
//  ViewController.m
//  test012701
//
//  Created by dxl on 2020/1/27.
//  Copyright © 2020 dxl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property(strong, nonatomic) NSDictionary *pickerData;
@property(strong, nonatomic) NSArray *provinceArray;
@property(strong, nonatomic) NSArray *cityArray;
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"provinces_cities" ofType:@"plist"];
    _pickerData = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
    
    _provinceArray = [_pickerData allKeys];
    
    _cityArray = [_pickerData objectForKey:_provinceArray[0]];
}

- (IBAction)buttonClicked:(id)sender {
    _resultLabel.text = [NSString stringWithFormat:@"%@%@市", _provinceArray[[_pickView selectedRowInComponent:0]], _cityArray[[_pickView selectedRowInComponent:1]]];
}



- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    if (component == 0) {
        return [_provinceArray count];
    }
    return [_cityArray count];
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0 ) {
        return _provinceArray[row];
    }
    return _cityArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        _cityArray = [_pickerData objectForKey:_provinceArray[row]];
        [pickerView selectRow:0 inComponent:1 animated:NO];
        [pickerView reloadComponent:1];
        
    }
    
    
}

@end

image.png
上一篇 下一篇

猜你喜欢

热点阅读