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

***控制层

#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
时间: 2024-07-31 12:56:30

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

iOS 8 &amp; Xcode 6:UINib加载xib文件问题

使用UINib类静态方法nibWithNibName:bundle:加载xib(nib)文件,第一个参数无须加文件后缀.若加后缀,则程序报错:第二个参数为空时,程序从mainBundle指向的路径中搜索文件. 正确的写法: UINib *storyCellNib = [UINib nibWithNibName:@"StoryCell" bundle:nil]; [self.tableView registerNib:storyCellNib forCellReuseIdentifier

ios – 使用UINib加载xib文件实现UITableViewCell

xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xib文件的加载过程: 1.将xib文件从磁盘载入内存,有两种技术可以加载xib文件:NSBundle和UINib. 2.执行unarchive和initialize操作,该过程主要由NSCoding Protocol中的initWithCoder:(NSCoder *)decoder完成. 3.建立c

ios UI加载xib文件到控制器的两种方式(MS)

X-code6.3 创建xib文件 加载xib文件到控制器的两种方式(MS) // 1.第一种方式 //    NSArray * array = [[NSBundle mainBundle]loadNibNamed:@"cell" owner:nil options:nil]; //    MyTableViewCell * cell = [array firstObject]; 其中cell为新建的xib文件名 // 2.第二种方式 UINib * nib = [UINib nib

ios &ndash; 使用UINib加载xib文件实现UITableViewCell

  xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xib文件的加载过程:   1.将xib文件从磁盘载入内存,有两种技术可以加载xib文件:NSBundle和UINib.   2.执行unarchive和initialize操作,该过程主要由NSCoding Protocol中的initWithCoder:(NSCoder *)decoder完成.

iOS加载xib方法

使用bundle加载xib,代码如下: UIView *appInfoView = [[[NSBundle mainBundle] loadNibNamed:@"xibName" owner:nil options:nil] lastObject]; [self.view addSubview:appInfoView]; [NSBundle mainBundle]会返回当前bundle的路径:bundle是什么呢?请参考:ios开发里面的bundle是什么鬼 loadNibNamed方

[转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

转载地址:http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要,所以,需要创建一个没有storyboard的项目 1. 创建一个新的工程 2. 选择仅一个视图的模板 选择 Single View Application , 点击Next 3. 填写项目信息 不需要选择core data,填好信息后,点击next,然后点击create 4. 删除sto

iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

---恢复内容开始--- 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要,所以,需要创建一个没有storyboard的项目 1. 创建一个新的工程 2. 选择仅一个视图的模板 选择 Single View Application , 点击Next 3. 填写项目信息 不需要选择core data,填好信息后,点击next,然后点击create 4. 删除storyboard和launchscreen.xib文件 将storyboard和l

iOS开发——图形编程Swift篇&amp;CAShapeLayer实现圆形图片加载动画

CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形渐现动画结合.这个组合的效果有趣,独一无二和有点迷人. 这个教程将会教你如何使用Swift和Core Animatoin来重新创建这个效果.让我们开始吧! 基础 首先下载这个教程的启动项目,然后编译和运行.过一会之后,你应该看到一个简单的image显示: 这 个启动项目已经预先在恰当的位置将view

IOS加载PDF文件

今天的任务是:在iOS上加载显示pdf文件. 方法一:利用webview [cpp] view plaincopy -(void)loadDocument:(NSString *)documentName inView:(UIWebView *)webView { NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil]; NSURL *url = [NSURL fileURLWithPath