热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

iOS开发中DatePicker和UIToolBar控件的使用简介

这篇文章主要介绍了iOS开发中DatePicker和UIToolBar控件的使用简介,代码基于传统的Objective-C,需要的朋友可以参考下

一、Date Picker控件
1.简单介绍:

201611592936439.png (549×278)

Date Picker显示时间的控件
有默认宽高,不用设置数据源和代理
如何改成中文的?
(1)查看当前系统是否为中文的,把模拟器改成是中文的
(2)属性,locale选择地区
如果默认显示不符合需求。时间有四种模式可以设置,在model中进行设置
时间可以自定义(custom)。
设置最小时间和最大时间,超过就会自动回到最小时间。
最大的用途在于自定义键盘:弹出一个日期选择器出来,示例代码如下:
 
 2.示例代码

代码如下:

//
//  YYViewController.m
//  datepicker
//
//  Created by apple on 14-6-3.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
/**
 *  文本输入框
 */
@property (strong, nonatomic) IBOutlet UITextField *textfield;

@end


代码如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //1
    //添加一个时间选择器
    UIDatePicker *date=[[UIDatePicker alloc]init];
    /**
     *  设置只显示中文
     */
    [date setLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
    /**
     *  设置只显示日期
     */
    date.datePickerMode=UIDatePickerModeDate;
//    [self.view addSubview:date];
   
    //当光标移动到文本框的时候,召唤时间选择器
    self.textfield.inputView=date;
   
    //2
    //创建工具条
    UIToolbar *toolbar=[[UIToolbar alloc]init];
    //设置工具条的颜色
    toolbar.barTintColor=[UIColor brownColor];
    //设置工具条的frame
    toolbar.frame=CGRectMake(0, 0, 320, 44);
   
    //给工具条添加按钮
        UIBarButtonItem *item0=[[UIBarButtonItem alloc]initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(click) ];
   
        UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
   
        UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
   
     toolbar.items = @[item0, item1, item2, item3];
    //设置文本输入框键盘的辅助视图
    self.textfield.inputAccessoryView=toolbar;
}
-(void)click
{
    NSLog(@"toolbar");
}
@end


实现效果:

201611593003027.png (321×497)

二、UITool Bar
在上面可以添加子控件TOOLBAR中只能添加UIBarButtonItem子控件,其他子控件会被包装秤这种类型的
上面的控件依次排放(空格————)
有样式,可以指定样式(可拉伸的),一般用来做工具栏。
 
使用toolbar做点菜的头部标题
如何让点菜系统居中?在ios6中是正的,在ios7中是歪的
在自定义键盘上加上一个工具栏。
数组里什么顺序放的,就按照什么顺序显示
  toolbar.items = @[item0, item1, item2, item3];
    //设置文本输入框键盘的辅助视图
    self.textfield.inputAccessoryView=toolbar;

好,让我们仔细来看一下UITool Bar的用法。
1.首先,我们看一下UIBbarButtonItem有哪些初始化方法,这也可以看出,它可以被定义为什么东东,然后加到UIToolBar上面去。

根据SDK的文档,我们可以发现UIBarButtonItem有如下几种初始化的方法:

代码如下:

-initWithTitle(添加button用这个)

-initWithImage

-initWithBarButtonSystemItem(添加系统自定义的button,形状跟大小都已经固定了)下面链接里面有按钮图片样式

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html

-initWithCustomView(添加除了button以外的View)


第4种方法就是我们添加各种作料的接口,所以今天的主角其它也是它。

2.在UIToolBar上面添加Title

代码如下:

UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame: 

                                                    CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)]; 

                                                     

NSMutableArray *myToolBarItems = [NSMutableArray array]; 

[myToolBarItems addObject:[[[UIBarButtonItem alloc] 

                                                        initWithTitle:@"myTile"  

                                                        style:UIBarButtonItemStylePlain  

                                                        target:self  

                                                        action:@selector(action)] autorelease]]; 

[myToolBar setItems:myToolBarItems animated:YES]; 

[myToolBar release]; 

[myToolBarItems];                                                        


 

setItems传入值或者说items是一个对象数组。

3.在UIToolBar上面添加image

代码如下:

[myToolBarItems addObject:[[[UIBarButtonItem alloc] 

                                        initWithImage:[UIImage imageNamed:@"myImage.png"]  

                                        style:UIBarButtonItemStylePlain  

                                        target:self  

                                        action:@selector(action)]];  

4.在UIToolBar上面添加SystemItem

[myToolBarItems addObject:[[[UIBarButtonItem alloc] 

                                        initWithBarButtonSystemItem:UIBarButtonSystemItemPlay  

                                        target:self  

                                        action:@selector(action)] autorelease]];  


Note:

initWithBarButtonSystemItem初始化:

代码如下:

- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action

Defines system defaults for commonly used items.

typedef enum { 

    UIBarButtonSystemItemDone, 

    UIBarButtonSystemItemCancel, 

    UIBarButtonSystemItemEdit, 

    UIBarButtonSystemItemSave, 

    UIBarButtonSystemItemAdd, 

    UIBarButtonSystemItemFlexibleSpace, 

    UIBarButtonSystemItemFixedSpace, 

    UIBarButtonSystemItemCompose, 

    UIBarButtonSystemItemReply, 

    UIBarButtonSystemItemAction, 

    UIBarButtonSystemItemOrganize, 

    UIBarButtonSystemItemBookmarks, 

    UIBarButtonSystemItemSearch, 

    UIBarButtonSystemItemRefresh, 

    UIBarButtonSystemItemStop, 

    UIBarButtonSystemItemCamera, 

    UIBarButtonSystemItemTrash, 

    UIBarButtonSystemItemPlay, 

    UIBarButtonSystemItemPause, 

    UIBarButtonSystemItemRewind, 

    UIBarButtonSystemItemFastForward, 

    UIBarButtonSystemItemUndo,        // iPhoneOS 3.0 

    UIBarButtonSystemItemRedo,        // iPhoneOS 3.0 

} UIBarButtonSystemItem; 


5.在UIToolBar上面添加其它各种控件,最自由意义,最有意思的,我把它放在最后来讲。我们使用initWithCustomView来完成,

这里需要看一下initWithCustomView的定义:

代码如下:

- (id)initWithCustomView:(UIView *)customView

可以看出,它的参数是一个VIEW,所以我们给它的配料要正确哦才行哦,否则,你就等着时间DIDADIDA的流失吧.

A>加一个开关switch:

代码如下:

[myToolBarItems addObject:[[[UIBarButtonItem alloc]    

                                initWithCustomView:[[[UISwitch alloc] init] autorelease]] 

                                    autorelease]]; 


B>加一个按钮UIBarButtonItem
代码如下:

UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] 

                                 initWithTitle:@"myButton" 

                                 style:UIBarButtonItemStyleBordered 

                                 target:self  

                                 action:@selector(action)]autorelease]; 

get1Button.width = 50; 

[myToolBarItems addObject:myButton];     


C>加一个文本Label
代码如下:

view plaincopy to clipboardprint?

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)]; 

myLabel.fOnt=[UIFont systemFontOfSize:10]; 

//myLabel.backgroundColor = [UIColor clearColor]; 

//myLabel.textAlignment=UITextAlignmentCenter; 

UIBarButtonItem *myButtOnItem= [[UIBarButtonItem alloc]initWithCustomView:myLabel]; 

[myToolBarItems addObject: myButtonItem];    

[mylabel release]; 

[myButtonItem release]; 


 

D>加一个进度条UIProgressView

代码如下:

UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)]; 

UIBarButtonItem *myButtOnItem= [[UIBarButtonItem alloc]initWithCustomView:myProgress]; 

[myToolBarItems addObject: myButtonItem]; 

[myProgress release];                                            

[myButtonItem release]; 


可以加使用initWithCustomView制作各种button,这里就不在这里一个一个在加了。我想你应该也已经掌握了如何添加各种buttonItem的方法了。


推荐阅读
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Lodop中特殊符号打印设计和预览样式不同的问题解析
    本文主要解析了在Lodop中使用特殊符号打印设计和预览样式不同的问题。由于调用的本机ie引擎版本可能不同,导致在不同浏览器下样式解析不同。同时,未指定文字字体和样式设置也会导致打印设计和预览的差异。文章提出了通过指定具体字体和样式来解决问题的方法,并强调了以打印预览和虚拟打印机测试为准。 ... [详细]
  • Final关键字的含义及用法详解
    本文详细介绍了Java中final关键字的含义和用法。final关键字可以修饰非抽象类、非抽象类成员方法和变量。final类不能被继承,final类中的方法默认是final的。final方法不能被子类的方法覆盖,但可以被继承。final成员变量表示常量,只能被赋值一次,赋值后值不再改变。文章还讨论了final类和final方法的应用场景,以及使用final方法的两个原因:锁定方法防止修改和提高执行效率。 ... [详细]
  • 本文介绍了求解gcdexgcd斐蜀定理的迭代法和递归法,并解释了exgcd的概念和应用。exgcd是指对于不完全为0的非负整数a和b,gcd(a,b)表示a和b的最大公约数,必然存在整数对x和y,使得gcd(a,b)=ax+by。此外,本文还给出了相应的代码示例。 ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 电销机器人作为一种人工智能技术载体,可以帮助企业提升电销效率并节省人工成本。然而,电销机器人市场缺乏统一的市场准入标准,产品品质良莠不齐。创业者在代理或购买电销机器人时应注意谨防用录音冒充真人语音通话以及宣传技术与实际效果不符的情况。选择电销机器人时需要考察公司资质和产品品质,尤其要关注语音识别率。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 如何去除Win7快捷方式的箭头
    本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
author-avatar
上海千恩万燮
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有