zoukankan      html  css  js  c++  java
  • UIDatePicer&键盘的处理

    "【案例: DatePicker&键盘处理】"

    》掌握DatePicker本地化的设置

    (1)storyboard设置本地化

    (2)获取系统可以的本地化[NSLocale availableLocaleIdentifiers]

    (3)代码设置日期的本地化

    》掌握DataPikcer的日期格式设置

    (1)storybard设置日期格式

    (2)代码设置日期格式 datePickerMode属性

    》掌握UITextField如果弹出日期选择器并在键盘添加工具条

    (1)设置UITextField的inputView属性为日期选择器即可

    (2)熟悉UIToolbar的使用,添加UIBarButtonItem

    (3)自定义一个键盘工具条,添加在键盘上

    UITextField,设置的inputAccessoryView属性

    (4)通过代理监听键盘工具条的几个按钮

    》熟悉代码实现UIToolBar

    "注:

    "1.创建ToolBar要设置frm

    "2.添加固定弹簧时,一定要设置宽度

    Launchxxx.xib 运行出现红色,因为项目名称有个特殊符号“&”,导致编译时出现问题。但在iOS9就没有出现问题。

    Main.story.board

    一个是固定弹簧,一个是灵活弹簧。

    ViewController.m

    //

    //  ViewController.m

    //  DatePicker&键盘的处理

    //

    //  Created by huan on 16/1/13.

    //  Copyright © 2016 huanxi. All rights reserved.

    //

     

    #import "ViewController.h"

    #import "CZKeyboardToolbar.h"

    @interface ViewController ()<CZKeyboardToolbarDelegate>

    @property (strong, nonatomic) UIDatePicker *datePicker;

     

    @property (weak, nonatomic) IBOutlet UITextField *textField;

     

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        //创建datepicker

        self.datePicker = [[UIDatePicker alloc] init];

        //设置日期控制的本地化

    //    NSLog(@"%@",[NSLocale availableLocaleIdentifiers]);

        self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];

        //日期控件格式

        self.datePicker.datePickerMode = UIDatePickerModeDate;

        //设置textfield的键盘

        self.textField.inputView = self.datePicker;

        CZKeyboardToolbar *toolbar = [CZKeyboardToolbar toolbar];

        //设置键盘的代理

        toolbar.kbdelegate = self;

        

        /**

         * 代码创建toolbar

         */

     

    //    UIToolbar *toolbar = [[UIToolbar alloc] init];

    //    toolbar.backgroundColor = [UIColor grayColor];

    //    //屏幕的宽度

    //    CGFloat screenW = [[UIScreen mainScreen] bounds].size.width;

    //    toolbar.bounds = CGRectMake(0, 0, screenW, 44);

    //    UIBarButtonItem *previousBtn = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil];

    //    UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:nil action:nil];

    //    UIBarButtonItem *downBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:nil action:nil];

    //    //固定长度的按钮

    //    UIBarButtonItem *fixedBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    //    //代码实现要设置宽度

    //    fixedBtn.width = 10;

    //    //可拉伸的按钮

    //    UIBarButtonItem *flexable = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    //    

    //   

    //    

    //    //添加UIToolbar里面的按钮

    //    toolbar.items = @[previousBtn, fixedBtn, nextBtn, flexable, downBtn];

        //设置textfield 的辅助工具条

        self.textField.inputAccessoryView = toolbar;

      //代码创建UIToolbar

    }

    #pragma mark 自定义键盘工具条的代理方法

    -(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidselected:(UIBarButtonItem *)item{

        if (item.tag == 2) {

            //获取日期显示在textField

            NSDate *date = self.datePicker.date;

            NSLog(@"%@", date);

            

            //日期转字符串

            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

            //设置日期格式

            dateFormatter.dateFormat = @"yyyyMMdd";

            NSString *dateStr = [dateFormatter stringFromDate:date];

            self.textField.text = dateStr;

        }

        

        

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

    下面是封装的类,减少代码量

    CZKeyboardToolbar.h

    //

    //  CZKeyboardToolbar.h

    //  DatePicker&键盘的处理

    //

    //  Created by huan on 16/1/13.

    //  Copyright © 2016 huanxi. All rights reserved.

    //

     

    #import <UIKit/UIKit.h>

    @class CZKeyboardToolbar;

    @protocol CZKeyboardToolbarDelegate<NSObject>

    @optional

    /**

     * Item.tag == 0 表示上一页 1 下一页 2 Down

     */

    -(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidselected:(UIBarButtonItem *)item;

    @end

    @interface CZKeyboardToolbar : UIToolbar

     

    +(instancetype) toolbar;

    @property (weak, nonatomic) id<CZKeyboardToolbarDelegate>kbdelegate;//为什么不是delegate,因为UIToolbar 有属性delegate

     

    @end

    CZKeyboardToolbar.m

    //

    //  CZKeyboardToolbar.m

    //  DatePicker&键盘的处理

    //

    //  Created by huan on 16/1/13.

    //  Copyright © 2016 huanxi. All rights reserved.

    //

     

    #import "CZKeyboardToolbar.h"

    @interface CZKeyboardToolbar()

     

     

     

     

    @end

    @implementation CZKeyboardToolbar

     

    /*

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    - (void)drawRect:(CGRect)rect {

        // Drawing code

    }

    */

    +(instancetype) toolbar{

        return [[[NSBundle mainBundle] loadNibNamed:@"CZKeyboardToolbar" owner:nil options:nil] lastObject];

    //因为xib的控件可以多拖几个,是同一级的

    }

     

     

     

    - (IBAction)itemBtnclick:(id)sender {

        //判断代理有没有实现方法

        if([self.kbdelegate respondsToSelector:@selector(keyboardToolbar:btndidselected:)]){

            [self.kbdelegate keyboardToolbar:self btndidselected:sender];

        }

    }

    @end

    CZKeyboardToolbar.xib

     

    在同一控制器中如果选中,那么不用代理,但是如果你用控件来改变界面显示的值,可以用代理。仔细体会一下。

  • 相关阅读:
    搭建好lamp,部署owncloud。
    部署LAMP环境搭建一个网站论坛平台
    二进制编译安装httpd服务
    安装httpd服务并配置
    FTP的应用
    Linux配置IP,安装yum源
    RHEL-server-7.0-Linux-centos安装过程
    zabbix监控某一进程
    python获取windows系统的CPU信息。
    python相关cmdb系统
  • 原文地址:https://www.cnblogs.com/Lu2015-10-03/p/5128436.html
Copyright © 2011-2022 走看看