美文网首页
iOS学习-3

iOS学习-3

作者: Ello_Orld | 来源:发表于2020-01-27 22:18 被阅读0次

    1. 日期选择器

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

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

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

    image.png

    看一下datePicker属性:


    image.png
    • Mode 对应上面四种模式
    • Locale 选择指定区域的日期格式
    • Interval 日期分辨率
    • Date 默认选择日期
      下面还有最小和最大日期的可选约束。
      拖拽相应的控件到代码中,实现选择日期后,点击按钮,uilabel展示选择的日期
    //
    //  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

    相关文章

      网友评论

          本文标题:iOS学习-3

          本文链接:https://www.haomeiwen.com/subject/wmgethtx.html