zoukankan      html  css  js  c++  java
  • IOS学习笔记(二)UIDatePicker

    控件长宽尽量用变量来定义,不要直接用数值。

    1.textField中有一设置显示灰色提示字样的方法    setPlaceholder:(NSString *) ;  <

    2.当选中textField控件时,弹出新视图,将新视图设置为日期选中控件。即选中textField则弹出日期选择控件 [textField setInputView:dataPicker];  <

    3.从本地加载文件时一般方式:

        NSString *gifpath = [[NSBundle mainBundle] pathForResource:@"ball" ofType:@"jpg"];

        FLAnimatedImage *image = [[FLAnimatedImage alloc] initWithAnimatedGIFData:[NSData dataWithContentsOfFile:gifpath]];

    加载文件一般通过路径索引,所以先定义path,在用path去加载。

    UIDatePicker  日期选择控件

    1.模式:日期-时间, 日期, 时间。

    设置模式为日期   [dataPicker setDatePickerMode:UIDatePickerModeDate]; <

    设置Local,即显示语言。将语言设置为中文  [datePicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"]];  <

    监听日期选择控件数值变化  [datePicker addTarget:self action:(SEL) forControlEvents:UIControlEventValueChanged]; <

    UIPickView   主要功能是从指定的数据源中选择数据,即从各种格式的数据源选择数据。使用PickView之前需要指定数据源。

    数据源方法:

      - (NSInteger)numberOfComponentsInPickerView:       <          返回总行数

      -(NSInteger)pickerView:   numberOfRowsInComponent:     <    返回第component列的行数

      [pickerView reloadComponent:1;     <  刷新数据方法

    代理需添加协议 <UIPickerViewDataSource, UIPickerViewDelegate>

    代理可以给UIPickerView提供具体的数据、监听UIPickerView的一些事件   。   代理必须遵守UIPickerViewDelegate协议,常用代理方法如下:

      - (NSString *)pickerView:   titleForRow:  forComponet:        <       第component列第row行显示什么字符串内容

      -(void) pickerView:   didSelectRow:  inComponent:                <       每当选择了新的一行就会调用

    代理方法扩展

      -(UIView *)pickerView:   viewForRow:   forComponent:   reusingView:     <   第coomponent列第row行显示什么样的UIView,最后一个view参数是用来性能优化的

      此处返回值同样可以是NSString     等等对象。

      -(CGFloat)pickerView:   rowHeightForComponent:            <    返回第component列每一行的高度  

    用plist文件初始化一个数组:   NSString *path = [NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"];        

                  [_dataList = [NSArray arrayWithContentOfFile:path];

    设置选中指示器       [picker setShowsSelectionIndicator:YES];      

    ios中,很多视图的高度均习惯性设置成  44  ,需注意。

    1.直接 NSLog(@"%@", datePicker);  可将datePicker的属性进行输出。

    2.将日期输出     [datePicker.date.description]   此方式粗糙显示信息,一般不用。

    要转换日期格式,需要使用NSDateFormatter, 专门用来转换日期格式的对象 。

    先设置日期的格式字符串:NSDateformatter *formatter = [[NSDateFormatter alloc] init]; 

                 [formatter setDateFormat:@"yyyy-MM-dd"];    

    然后使用格式字符串,将日期转换成字符串:NSString *dateString = [formatter stringFromDate:datePicker.date]

    3.设置日期控件的初始值

        NSString *dateString = @"1949-10-1";

        NSDateformatter *formatter = [[NSDateFormatter alloc] init];

        [formatter setDateformat:@"yyyy-MM-dd"];

        NSDate *date = [formatter dateFromString:dateString];

        [datePicker setDate:date];

  • 相关阅读:
    鼠标的移动触发函数改变字体颜色
    Godaddy创始人:成就亿万富翁的10条规则
    关于编程,大学没有传授的十件事
    Using XAMPP for Local WordPress Theme Development
    100+ Resources for Web Developer
    你必须非常努力,才能看起来毫不费力
    建立WordPress博客网站——个人教程
    函数指针和指针函数
    每天写出好代码的5个建议
    LumiSoft Mail Server
  • 原文地址:https://www.cnblogs.com/fsliu/p/4237773.html
Copyright © 2011-2022 走看看