美文网首页
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

相关文章

  • 2016年度总结

    2016年—年终总结 学习工作 1.iOS: ****1月-3月****:学习了 iOS 基础知识,包括 UITa...

  • 3D Touch

    iOS之实现3D Touch跟着官方文档学习3D Touch

  • iOS学习-3

    1. 日期选择器 UIDataPicker 日期选择器有四种模式: 日期日期时间时间倒计时 打开故事板文件,拖拽...

  • 使用Core Data出现Missing current ver

    @(iOS学习)[笔记] Xcode 7 beta 3使用Core Data出现 Missing current ...

  • 优秀博客、技术文章收藏帖

    1、 理解 iOS 的内存管理By唐巧2、 资源帖-优秀博客、iOS开发技术文、学习网站3、 iOS10消息推送4...

  • ios 资料大全

    一、IOS资料汇总 1.iOS 资料合集2.iOS超全开源框架、项目和学习资料汇总(3)网络和Model篇 二、I...

  • iOS学习笔记3

    Plist文件 加载Plist文件先获取文件路径在将Plist文件传入数组或者字典中 常见问题 项目中某个.m文件...

  • IOS学习(3)-UILabel

    UILabeliOS 在UILabel显示不同的字体和颜色 让UILabel自适应文本长度和宽度 1. 先写文本尺...

  • 【iOS逆向】一、逆向内容简介与越狱环境搭建

    总体内容 1、逆向课程简介2、学习逆向的条件3、iOS越狱(iOS Jailbreak)的优点和缺点4、完美越狱和...

  • iOS 资料文档总集

    iOS资料文档总集 iOS资料 iOS学习社区 iOS总结1 iOS总结2 iOS扩展 RunTime学习 Git...

网友评论

      本文标题:iOS学习-3

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