码迷,mamicode.com
首页 > 移动开发 > 详细

IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)

时间:2015-08-19 19:38:38      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

***控制层

#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.
    
    //创建datapikcer
    self.datepicker = [[UIDatePicker alloc] init];
    
    //本地方
    self.datepicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    
    //日期控件格式
    self.datepicker.datePickerMode = UIDatePickerModeDate;
    
    
    //设置textfield的键盘
    self.textField.inputView = self.datepicker;
    
    
    CZKeyboardToolbar *toolbar = [CZKeyboardToolbar toolbar];
    
    //设置键盘的代理
    toolbar.kbDelegate = self;
    
    //设置textfield的辅助工具条
    self.textField.inputAccessoryView = toolbar;
}

#pragma mark 自定义键盘工具条的代理方法
-(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item{

    
    if (item.tag == 2) {//Done按钮点击
        //获取日期显示在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;

//隐藏键盘

[self.birthdayFiled resignFirstResponder];

    }
    

}

/**
 *  代码创建的toolbar
 */
-(void)codeForToolbar{
    //代码创建UIToolbar
    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 *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    
    //固定长度的按钮
    UIBarButtonItem *fixedBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //代码实现要设置宽度
    fixedBtn.width = 10;
    
    
    //可拉伸的按钮
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    //添加UIToolbar里面的按钮
    toolbar.items = @[previousBtn,fixedBtn,nextBtn,flexible,doneBtn];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

***CZKeyboardToolbar.m

#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];
}


- (IBAction)itemBtnClick:(id)sender {
    
    //判断代理有没有实现方法
    if ([self.kbDelegate respondsToSelector:@selector(keyboardToolbar:btndidSelected:)]) {
        [self.kbDelegate keyboardToolbar:self btndidSelected:sender];
    }
    
}
@end

***CZKeyboardToolbar.h

#import <UIKit/UIKit.h>

@class CZKeyboardToolbar;
@protocol CZKeyboardToolbarDelegate <NSObject>

@optional

/**
 *  item.tag  0 表示上一个按钮 1:下一个按钮 2:done完成按钮
 */
-(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item;

@end

@interface CZKeyboardToolbar : UIToolbar

+(instancetype)toolbar;

@property (weak, nonatomic) id<CZKeyboardToolbarDelegate> kbDelegate;//键盘的代理

@end

 

IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)

标签:

原文地址:http://www.cnblogs.com/ios-g/p/4742795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!