IOS_DatePicker_PickerView_SegmentControl_键盘处理
H:/0712/01_UIController_MJViewController.m
// MJViewController.m
// 01-总结复习
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*---------------------------------------
UIControl继承自UIView
仅仅有继承UIControl的控件,才干监听事件.
addTarget方法
--------------------------------------
注意,下面关系不是继承:
控制器ViewController
|--View Xcode中经常使用的快捷键
Xcode中各种图标的含义
viewControll中的FirstResponder
*/
#import "MJViewController.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.center = CGPointMake(100, 100);
[self.view addSubview:btn];
}
@end
H:/0712/02_代码加入1行删除1行_MJViewController.h
//
// MJViewController.h
// 02-联系人信息管理-通过代码
//
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*
-----------------------------------
加入控件的方式: -----------------------------------
1.加入图片资源
须要勾选:Destination + Add to targets(2个都要勾选)
2.搭建UI界面
3.监听加入button事件
4.监听删除button事件 ---------------------------------
动态加入一行:
1.首先创建一行UIView
2.把创建的一行,加入到控制器的UIView中去
*/ /*
1.加入行,每一行有相应的子控件
2.删除行
*/ #import <UIKit/UIKit.h> @interface MJViewController : UIViewController
// UIBarButtonItem 删除 storyBoard里面将其enabled复选框去掉
@property (nonatomic, weak) IBOutlet UIBarButtonItem *removeItem; // 响应button点击,加入一行
- (IBAction)addRow; // 响应button点击,删除一行
- (IBAction)removeRow; @end
H:/0712/02_代码加入1行删除1行_MJViewController.m
// MJViewController.m
// 02-联系人信息管理-通过代码
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*
-----------------------------------
加入控件的方式: -----------------------------------
1.加入图片资源
须要勾选:Destination + Add to targets(2个都要勾选)
2.搭建UI界面
3.监听加入button事件
4.监听删除button事件 ---------------------------------
动态加入一行:
1.首先创建一行UIView
2.把创建的一行,加入到控制器的UIView中去
*/
#import "MJViewController.h"
// 常量:行高
#define kRowHeight 70
@interface MJViewController ()
@end
@implementation MJViewController
#pragma mark 响应button点击,加入一行
- (IBAction)addRow
{
// 0.得到当前的最后一个子控件,须要它的y坐标和高度
UIView *lastChild = [self.view.subviews lastObject];
// 1.创建新的一行(UIView)
UIView *newRow = [[UIView alloc] init];
// 得到控制器的view的宽度,屏幕宽
CGFloat viewWidth = self.view.frame.size.width;
// 计算新的一行的Y值 == 最后一个子控件的Y + 最后一个子控件的高度 + 1
CGFloat newRowY = lastChild.frame.origin.y + lastChild.frame.size.height + 1;
// 设置新行的尺寸和位置
newRow.frame = CGRectMake(0, newRowY, viewWidth, kRowHeight);
// 设置背景颜色
newRow.backgroundColor = [UIColor grayColor];
// 2.加入新的一行到控制器的view中
[self.view addSubview:newRow];
// 3.让删除item可用(变得可被点击),默认的storyBoard里面没有勾选enabled
self.removeItem.enabled = YES;
// 4.继续往新的一行(newRow)里面加入子控件,如图片和文字
// 4.1 加入名字标签
UILabel *nameView = [[UILabel alloc] init];
nameView.text = @"3213124324";
// 设置center(让图片和文字在父控件RowView里面居中)
nameView.center = CGPointMake(viewWidth * 0.5, kRowHeight * 0.5);
// 设置bounds(设置宽高,不影响位置)
nameView.bounds = CGRectMake(0, 0, 200, 40);
// 设置Lable里面的文字居中
nameView.textAlignment = NSTextAlignmentCenter;
// 清除Lable的背景颜色
nameView.backgroundColor = [UIColor clearColor];
[newRow addSubview:nameView];
// 4.2 加入图片到新的一行里面(newRow)
UIButton *iconView = [UIButton buttonWithType:UIButtonTypeCustom];
// 设置center
iconView.center = CGPointMake(40, kRowHeight * 0.5);
// 设置宽高
iconView.bounds = CGRectMake(0, 0, 44, 44);
// 随机生成图片名
// arc4random() // 随机生成一个正整数
int index = arc4random_uniform(8); // 生成[0, 8]的整数
NSString *imageName = [NSString stringWithFormat:@"%d.png", index];
UIImage *image = [UIImage imageNamed:imageName];
// 设置button的背景图片
[iconView setBackgroundImage:image forState:UIControlStateNormal];
[newRow addSubview:iconView];
}
#pragma mark 响应button点击,删除一行
- (IBAction)removeRow
{
// 1.取出最后一个子控件
UIView *lastChild = [self.view.subviews lastObject];
// 2.移除最后一个子控件
[lastChild removeFromSuperview];
// 3.设置删除item可不可用,简单版本号和优化版本号
/*
if (self.view.subviews.count == 1) {
self.removeItem.enabled = NO;
} else {
self.removeItem.enabled = YES;
}*/
// 假设仅仅剩下一个子控件,就让删除item不可用
// 假设还剩下多个子控件,就让删除item可用
self.removeItem.enabled = self.view.subviews.count > 1;
}
@end
H:/0712/03_代码加入1行删除1行2_MJViewController.h
//
// MJViewController.h
// 02-联系人信息管理-通过代码
//
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
// /*
1.加入行(行里面有子控件)
2.删除行
3.监听图片button点击
4.加入和删除的动画效果
*/ #import <UIKit/UIKit.h> @interface MJViewController : UIViewController
// UIBarButtonItem 删除 storyBoard里面将其enabled复选框去掉
@property (nonatomic, weak) IBOutlet UIBarButtonItem *removeItem; // 响应button点击,加入一行
- (IBAction)addRow; // 响应button点击,删除一行
- (IBAction)removeRow; @end
H:/0712/03_代码加入1行删除1行2_MJViewController.m
// MJViewController.m
// 02-联系人信息管理-通过代码
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
// 常量:行高
#define kRowHeight 70
#define kNameTag 5
#import "MJViewController.h"
@interface MJViewController ()
@end
@implementation MJViewController
#pragma mark 响应button点击,加入一行
- (IBAction)addRow
{
// 0.得到当前的最后一个子控件,须要它的y坐标和高度
UIView *lastChild = [self.view.subviews lastObject];
// 1.创建新的一行(UIView)
UIView *newRow = [[UIView alloc] init];
// 控制器的view的宽度,屏幕宽
CGFloat viewWidth = self.view.frame.size.width;
// 计算新的一行的Y值 == 最后一个子控件的Y + 最后一个子控件的高度 + 1
CGFloat newRowY = lastChild.frame.origin.y + lastChild.frame.size.height + 1;
// 设置尺寸和位置
newRow.frame = CGRectMake(0, newRowY, viewWidth, kRowHeight);
// 设置背景颜色
newRow.backgroundColor = [UIColor grayColor];
// 2.加入新的一行到控制器的view中
[self.view addSubview:newRow];
// 3.让删除item可用,默认的storyBoard里面没有勾选enabled
self.removeItem.enabled = YES;
// 4.继续往新的一行(newRow)里面加入子控件,如图片和文字
// 4.1 加入名字标签
UILabel *nameView = [[UILabel alloc] init];
nameView.text = [NSString stringWithFormat:@"mike---%d", self.view.subviews.count];
// 设置center(让图片和文字在父控件RowView里面居中)
nameView.center = CGPointMake(viewWidth * 0.5, kRowHeight * 0.5);
// 设置bounds(设置宽高。不影响位置)
nameView.bounds = CGRectMake(0, 0, 200, 40);
// 设置Lable里面的文字居中
nameView.textAlignment = NSTextAlignmentCenter;
// 设置Lable标签的tag,由于要拿到标签里面的文字---------------
nameView.tag = kNameTag;
// 清除Lable的背景颜色
nameView.backgroundColor = [UIColor clearColor];
[newRow addSubview:nameView];
// 4.2 加入图片到新的一行里面(newRow)
UIButton *iconView = [UIButton buttonWithType:UIButtonTypeCustom];
// 设置center
iconView.center = CGPointMake(40, kRowHeight * 0.5);
// 设置宽高
iconView.bounds = CGRectMake(0, 0, 44, 44);
// 随机生成图片名
// arc4random() // 随机生成一个正整数
int index = arc4random_uniform(8); // 生成[0, 8]的整数
NSString *imageName = [NSString stringWithFormat:@"%d.png", index];
UIImage *image = [UIImage imageNamed:imageName];
// 设置button的背景图片
[iconView setBackgroundImage:image forState:UIControlStateNormal];
// 绑定button的监听器
[iconView addTarget:self action:@selector(iconClick:)
forControlEvents:UIControlEventTouchUpInside];
[newRow addSubview:iconView];
// 5.搞一个动画
// 5.1 设置newRow的初始x为屏幕最右边
CGRect frame = newRow.frame;
frame.origin.x = viewWidth;
newRow.frame = frame;
// 5.2 设置newRow的x为0
frame.origin.x = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
// 这行代码才会有动画效果
newRow.frame = frame;
[UIView commitAnimations];
}
#pragma mark 监听并响应button图标的点击事件
- (void)iconClick:(UIButton *)icon
{
// 1.获取父控件
UIView *superview = icon.superview;
// 2.获取姓名标签
UILabel *nameView = (UILabel *)[superview viewWithTag:kNameTag];
NSLog(@"名称是----%@", nameView.text);
}
#pragma mark 响应button点击,删除一行
- (IBAction)removeRow
{
// 1.取出最后一个子控件
UIView *lastChild = [self.view.subviews lastObject];
// 2.利用block运行动画
[UIView animateWithDuration:0.3
animations:^{ // 存放须要运行动画的代码
CGRect frame = lastChild.frame;
frame.origin.x = self.view.frame.size.width;
lastChild.frame = frame;
}
completion:^(BOOL finished) {
NSLog(@"动画运行完成----");
[lastChild removeFromSuperview];
// 设置删除item可不可用
self.removeItem.enabled = self.view.subviews.count > 1;
}];
// 2.移除最后一个子控件
//[lastChild removeFromSuperview];
// 3.设置删除item可不可用
/*
if (self.view.subviews.count == 1) {
self.removeItem.enabled = NO;
} else {
self.removeItem.enabled = YES;
}*/
// 假设仅仅剩下一个子控件,就让删除item不可用
// 假设还剩下多个子控件,就让删除item可用
//self.removeItem.enabled = self.view.subviews.count > 1;
}
@end
H:/0712/04_xib加入1行删除1行_MJViewController.h
// MJViewController.h
// 02-联系人信息管理-通过xib
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
#import <UIKit/UIKit.h>
@interface MJViewController : UIViewController
/*
为XIB中的button加入点击事件,标准步骤:
1,首先XIB中的,FILE'S OWNER选择为MJViewController
2,[bundle loadNibNamed:@"row" owner:self options:nil];
owner參数传入当前控制器的实例对象
3,在MJViewController.h头文件里准备一个方法IBAction,
而且,与XIB中的button完毕拖线
这样,当点击XIB中的button时,将调用FILE'S OWNER类,的实例对象的已经连线的IBAction方法
*/ // 响应图标button点击,弹出AlertView
- (IBAction)iconClick:(UIButton *)sender; // 响应button点击,加入新的一行
- (IBAction)addRow;
@end
H:/0712/04_xib加入1行删除1行_MJViewController.m
// MJViewController.m
// 02-联系人信息管理-通过xib
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
// xib == nib xcode interface builder
/*
为XIB中的button加入点击事件,标准步骤:
1,首先XIB中的,FILE'S OWNER选择为MJViewController
2,[bundle loadNibNamed:@"row" owner:self options:nil];
owner參数传入当前控制器的实例对象
3,在MJViewController.h头文件里准备一个方法IBAction,
而且,与XIB中的button完毕拖线
这样,当点击XIB中的button时,将调用FILE'S OWNER类,的实例对象的已经连线的IBAction方法
*/
#import "MJViewController.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (IBAction)addRow
{
// 1.载入row.xib文件
NSBundle *bundle = [NSBundle mainBundle];
// 将self传入到File's Owner。创建Objects以下的全部对象
// 这个NSArray中存放着Objects以下的全部对象
// owner:是对象,哪个类的对象呢,是file's owner那个类的对象
NSArray *objs = [bundle loadNibNamed:@"row" owner:self options:nil];
// 2.取出第0个,即为新的一行
UIView *newRow = objs[0];
// 3.设置位置,先取得lastChild,由于要用到它的y和高度
UIView *lastChild = [self.view.subviews lastObject];
CGFloat newRowY = lastChild.frame.origin.y + lastChild.frame.size.height + 1;
CGRect frame = newRow.frame;
frame.origin = CGPointMake(0, newRowY);
newRow.frame = frame;
// 4.加入新的一行到控制器的view
[self.view addSubview:newRow];
// 5.设置数据
UILabel *label = (UILabel *)[newRow viewWithTag:10];
label.text = [NSString stringWithFormat:@"jack-%d", arc4random_uniform(10)];
}
- (IBAction)iconClick:(UIButton *)sender {
UILabel *label = (UILabel *)[sender.superview viewWithTag:10];
NSLog(@"%@", label.text);
}
@end
H:/0712/05_UIDatePicker_MJViewController.h
//
// MJViewController.h
// 05-UIDatePicker
//
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
// #import <UIKit/UIKit.h> @interface MJViewController : UIViewController
// Lable标签
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
// 文本框
@property (weak, nonatomic) IBOutlet UITextField *myfield;
// 拖线,UIDatePicker的Value Changed事件
- (IBAction)dateChange:(UIDatePicker *)sender;
@end
H:/0712/05_UIDatePicker_MJViewController.m
// MJViewController.m
// 05-UIDatePicker
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "MJViewController.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// inputView设置键盘
//self.myfield.inputView = [UIButton buttonWithType:UIButtonTypeContactAdd];
UIDatePicker *picker = [[UIDatePicker alloc] init];
// 模式是日期
picker.datePickerMode = UIDatePickerModeDate;
// 本地化日期格式
picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
// inputView设置键盘
self.myfield.inputView = picker;
// inputAccessoryView设置键盘上面的工具条
self.myfield.inputAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
/*UIDatePicker *picker = nil;
picker.datePickerMode = UIDatePickerModeDate;
picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];*/
}
// 监听UIDatePicker的Value Changed事件
- (IBAction)dateChange:(UIDatePicker *)sender {
NSDate *date = sender.date;
// 实例化一个NSDateFormatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置日期格式(2010/10/10)
formatter.dateFormat = @"yyyy/MM/dd";
// 将NSDate转成NSString
NSString *dateStr = [formatter stringFromDate:date];
self.dateLabel.text = dateStr;
}
@end
H:/0712/06_UIPickerView_MJViewController.h
//
// MJViewController.h
// 06-UIPickerView
//
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*
UIPickView重要方法一览:
1,返回特定列中当前选中的行号
[pickerView selectedRowInComponent:0];
2,数据源方法仅仅有两个,各自是
UIPickerView中有多少列: numberOfComponentsInPickerView
numberOfRowsInComponent,特定列中有多少行
3,代理方法:
返回第component列第row行显示的字符串title
选中了某一行就会调用:didSelectRow:inComponent:
*/ #import <UIKit/UIKit.h> @interface MJViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> @end
H:/0712/06_UIPickerView_MJViewController.m
//
// MJViewController.m
// 06-UIPickerView
//
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*
UIPickView重要方法一览:
1,返回特定列中当前选中的行号
[pickerView selectedRowInComponent:0];
2,数据源方法仅仅有两个,各自是
UIPickerView中有多少列: numberOfComponentsInPickerView
numberOfRowsInComponent,特定列中有多少行
3,代理方法:
返回第component列第row行显示的字符串title
选中了某一行就会调用:didSelectRow:inComponent:
*/
#import "MJViewController.h"
@interface MJViewController ()
@property (nonatomic, strong) NSArray *oneCol;
@property (nonatomic, strong) NSArray *twoCol;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 模拟第1列/组的数据
self.oneCol = @[@"00", @"01", @"02", @"03"];
// 模拟第2列/组的数据
self.twoCol = @[@"10", @"11", @"12"];
}
#pragma mark PickerView数据源方法:返回UIPickerView的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
#pragma mark PickerView数据源方法:返回在第component列共同拥有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return component == 0 ? self.oneCol.count : self.twoCol.count;
} #pragma mark - UIPickerView的代理方法
#pragma mark 返回第component列第row行显示的字符串title
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0)
{ // 假设是第0列,返回oneCol数组中第row行的数据
return self.oneCol[row];
} else { // 假设是第1列,返回twoCol数组中第row行的数据
return self.twoCol[row];
}
//return @"hahahha";
} #pragma mark 选中了某一行就会调用:didSelectRow:inComponent:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// 假设选中了第0列
if (component == 0)
{
// 取出第0列第row行的数据
NSString *left = self.oneCol[row]; // 获得第1列当前选中的行号
int rightRow = [pickerView selectedRowInComponent:1]; // 取得第1列第rightRow行的数据
NSString *right = self.twoCol[rightRow]; NSLog(@"%@ - %@", left, right); } else { // 假设选中了第1列
NSString *right = self.twoCol[row]; // 获得第0列当前选中的行号
int leftRow = [pickerView selectedRowInComponent:0]; //
NSString *left = self.oneCol[leftRow];
NSLog(@"%@ - %@", left, right);
}
//NSLog(@"-------row=%d, col=%d", row, component);
}
@end
H:/0712/07_UIPickerView自己定义行_MJViewController.h
//
// MJViewController.h
// 07-UIPickerView-显示复杂内容
//
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*
UIPickView重要方法一览:
1,返回特定列中当前选中的行号
[pickerView selectedRowInComponent:0];
2,数据源方法仅仅有两个,各自是
UIPickerView中有多少列: numberOfComponentsInPickerView
numberOfRowsInComponent,特定列中有多少行
3,代理方法:
返回第component列第row行显示的字符串title
选中了某一行就会调用:didSelectRow:inComponent:
*/ #import <UIKit/UIKit.h> @interface MJViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> @end
H:/0712/07_UIPickerView自己定义行_MJViewController.m
// MJViewController.m
// 07-UIPickerView-显示复杂内容
// Created by apple on 13-7-12.
// Copyright (c) 2013年 itcast. All rights reserved.
/*
UIPickView重要方法一览:
1,返回特定列中当前选中的行号
[pickerView selectedRowInComponent:0];
2,数据源方法仅仅有两个,各自是
UIPickerView中有多少列: numberOfComponentsInPickerView
numberOfRowsInComponent,特定列中有多少行
3,代理方法:
返回第component列第row行显示的字符串title
选中了某一行就会调用:didSelectRow:inComponent:
返回第component列第row行数须要显示的UIView
返回某列的行高rowHeightForComponent
*/
#import "MJViewController.h"
@interface MJViewController ()
// 存放全部国旗数据
@property (nonatomic, strong) NSArray *flags;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 载入数据
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"flags.plist" ofType:nil];
self.flags = [NSArray arrayWithContentsOfFile:path];
}
#pragma mark 数据源方法:返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
#pragma mark 数据源方法:返回行数
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return self.flags.count;
}
#pragma mark 返回第component列第row行数须要显示的UIView
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView *myview = [[UIView alloc] init];
CGFloat myviewH = 40;
CGFloat myviewW = 200;
myview.bounds = CGRectMake(0, 0, myviewW, myviewH);
NSDictionary *dict = self.flags[row];
// 国家名称
UILabel *label = [[UILabel alloc] init];
CGFloat labelWidth = 120;
label.frame = CGRectMake(0, 0, labelWidth, myviewH);
label.text = dict[@"name"];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[myview addSubview:label];
// 国旗图片
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(labelWidth, 0, myviewW - labelWidth, myviewH);
imageView.image = [UIImage imageNamed: dict[@"icon"] ];
[myview addSubview:imageView];
return myview;
}
// 代理方法:返回某列的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component
{
return 70;
}
@end
H:/0712/segment_图片排列_images.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>010.png</string>
<string>011.png</string>
<string>012.png</string>
<string>013.png</string>
<string>014.png</string>
<string>015.png</string>
<string>016.png</string>
<string>017.png</string>
<string>018.png</string>
</array>
</plist>
H:/0712/segment_图片排列_MJViewController.h
// MJViewController.h
// 图片排列
// Created by mj on 13-7-8.
// Copyright (c) 2013年 itcast. All rights reserved. #import <UIKit/UIKit.h> @interface MJViewController : UIViewController
// 用于拖线,响应点击segmentControl时候
- (IBAction)columnChange:(UISegmentedControl *)sender; @end
H:/0712/segment_图片排列_MJViewController.m
// MJViewController.m
// 图片排列
// Created by mj on 13-7-8.
// Copyright (c) 2013年 itcast. All rights reserved.
#define kIconWidth 60
#define kIconHeight 60
#import "MJViewController.h"
@interface MJViewController ()
// 用于接收Plist文件里的数组
@property (nonatomic, strong) NSArray *images;
// 数组,存放的是:一个个UIImageView
@property (nonatomic, strong) NSMutableArray *imageViews;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.载入资源
NSString *path = [[NSBundle mainBundle] pathForResource:@"images.plist"
ofType:nil];
// 用成员数组记住plist文件里的数组
self.images = [NSArray arrayWithContentsOfFile:path];
// 分配空间,为可变数组加入UIImageView做准备
self.imageViews = [NSMutableArray array];
// 2.创建全部的UIImageView
for (int i = 0; i<self.images.count; i++)
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:self.images[i]];
[self.view addSubview:imageView];
[self.imageViews addObject:imageView];
}
// 3.自己定义方法,排列位置
[self columnChange:nil];
}
// 用于拖线,响应点击segmentControl时候
- (IBAction)columnChange:(UISegmentedControl *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
// segmentControl的索引是从0開始算的,而列数显示的是2 3 4,所以列数=索引+2
int column = sender.selectedSegmentIndex + 2;
// 经典:图片之间水平间距为:(屏幕宽-列数*每一个图片宽)/(列数+1)
CGFloat horMargin = (self.view.frame.size.width - column * kIconWidth)/(column + 1);
CGFloat verticalMargin = 10;
CGFloat startX = horMargin;
CGFloat startY = 60;
for (int i = 0; i<self.imageViews.count; i++)
{
// 经典~算出第i张图片,位置:row行col列
// 行号为i/列数
int row = i / column;
// 所在列号为i%列数
int col = i % column;
// 第i个的x坐标为:第0张的x + 所在的列数 * (图片宽 + 图片之间间距);
CGFloat x = startX + col * (kIconWidth + horMargin);
// 第i个的y坐标为:第0张的y + 所在的行数 * (图片高 + 图片之间间距);
CGFloat y = startY + row * (kIconHeight + verticalMargin);
CGRect frame = CGRectMake(x, y, kIconWidth, kIconHeight);
// 仅仅须要数组中取出第i张图片,又一次赋予frame坐标就可以
UIImageView *imageView = self.imageViews[i];
imageView.frame = frame;
}
[UIView commitAnimations];
}
@end
H:/0712/滑块改变图片_images.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>icon</key>
<string>0.png</string>
<key>title</key>
<string>在他面前,其它神马表情都弱爆了! </string>
</dict>
<dict>
<key>icon</key>
<string>1.png</string>
<key>title</key>
<string>为什么你们选车牌。非得跟自己过不去呢?</string>
</dict>
<dict>
<key>icon</key>
<string>2.png</string>
<key>title</key>
<string>下午一客户来维修电脑,当时哥打开电脑一开。就石化了</string>
</dict>
<dict>
<key>icon</key>
<string>3.png</string>
<key>title</key>
<string>二逼青年伤不起啊,有木有啊啊啊啊</string>
</dict>
<dict>
<key>icon</key>
<string>4.png</string>
<key>title</key>
<string>这也忒狠了</string>
</dict>
<dict>
<key>icon</key>
<string>5.png</string>
<key>title</key>
<string>哥们为什么选八号呢</string>
</dict>
<dict>
<key>icon</key>
<string>6.png</string>
<key>title</key>
<string>这年头的SB不少</string>
</dict>
<dict>
<key>icon</key>
<string>7.png</string>
<key>title</key>
<string>够慘不?</string>
</dict>
<dict>
<key>icon</key>
<string>8.png</string>
<key>title</key>
<string>亲,你能改下你的网名么?哈哈</string>
</dict>
<dict>
<key>icon</key>
<string>9.png</string>
<key>title</key>
<string>这是在挑战小偷的智商么?</string>
</dict>
<dict>
<key>icon</key>
<string>10.png</string>
<key>title</key>
<string>这两货一定是兄妹! </string>
</dict>
<dict>
<key>icon</key>
<string>11.png</string>
<key>title</key>
<string>熊孩子又调皮了</string>
</dict>
<dict>
<key>icon</key>
<string>12.png</string>
<key>title</key>
<string>这小姑娘吃个牛排比杀牛还费劲啊</string>
</dict>
<dict>
<key>icon</key>
<string>13.png</string>
<key>title</key>
<string>我太TMD机智了</string>
</dict>
<dict>
<key>icon</key>
<string>14.png</string>
<key>title</key>
<string>这是哪家电视台的,这么坑爹</string>
</dict>
<dict>
<key>icon</key>
<string>15.png</string>
<key>title</key>
<string>求大神把我P得让人一见倾心的那种</string>
</dict>
</array>
</plist>
H:/0712/滑块改变图片_MJViewController.h
// MJViewController.h
// 图片浏览器
// Created by mj on 13-7-8.
// Copyright (c) 2013年 itcast. All rights reserved.
#import <UIKit/UIKit.h>
@interface MJViewController : UIViewController
- (IBAction)nightMode:(UISwitch *)sender;
// 成员:UIImageView
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
// 成员:标签 图片名
@property (weak, nonatomic) IBOutlet UILabel *imageTitle;
// 成员:标签 图片编号
@property (weak, nonatomic) IBOutlet UILabel *imageNo;
// 成员:UISlider 滑块,调节图片大小
@property (weak, nonatomic) IBOutlet UISlider *slider;
// 成员:UIStepper-----------------
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
// 成员:弹出来的设置view
@property (weak, nonatomic) IBOutlet UIView *settingView;
// 响应滑块的滑动,改变图片及文字及编号
- (IBAction)imageChange:(UISlider *)sender;
// 响应设置button的点击,作用是从底部弹出设置view
- (IBAction)setting;
@end
H:/0712/滑块改变图片_MJViewController.m
// MJViewController.m
// 图片浏览器
// Created by mj on 13-7-8.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "MJViewController.h"
@interface MJViewController ()
// 字典数组,字典有三对,标题,编号和图片名
@property (nonatomic, strong) NSArray *images;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.载入资源文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"images.plist"
ofType:nil];
// 字典数组,字典有三对,标题,编号和图片名
self.images = [NSArray arrayWithContentsOfFile:path];
// 2.初始化控件
// 设置slider最大值,为字典数组的长度-1,由于slider最小值是从0開始算的
self.slider.maximumValue = self.images.count - 1;
// 设置slider最小值从0開始算
self.slider.minimumValue = 0;
// ---------------------------
self.stepper.minimumValue = 0;
// ---------------------------
self.stepper.maximumValue = self.images.count - 1;
// 3.初始化显示的图片
[self imageChange:nil];
} // 响应滑块的滑动,改变图片及文字及编号
- (IBAction)imageChange:(UISlider *)sender {
int value = sender.value;
// 设置控件的值---------------------------
self.stepper.value = value;
self.slider.value = value;
NSDictionary *dict = self.images[value];
// 设置标题
self.imageTitle.text = dict[@"title"];
// 设置页码
self.imageNo.text = [NSString stringWithFormat:@"%d/%d", value + 1, self.images.count];
// 设置图片
self.imageView.image = [UIImage imageNamed:dict[@"icon"]];
}
// 响应设置button的点击,作用是从底部弹出设置view
- (IBAction)setting {
[UIView beginAnimations:nil context:nil];
// viewHeight 实质是屏幕的高
CGFloat viewHeight = self.view.frame.size.height;
CGRect rect = self.settingView.frame;
if (rect.origin.y == viewHeight) {
// 假设设置view的Y等于屏幕高,说明在底下,所以要显示出来
rect.origin.y = viewHeight - rect.size.height;
} else {
// 说明设置view已经是显示的,所以要隐藏到屏幕底下去
rect.origin.y = viewHeight;
}
self.settingView.frame = rect;
[UIView commitAnimations];
}
// 响应设置view里面的开关事件
- (IBAction)nightMode:(UISwitch *)sender {
// 假设开关是开的,就使用夜间模式,即深灰色
self.view.backgroundColor = sender.isOn?[UIColor darkGrayColor]:
[UIColor whiteColor];
}
@end
H:/0712/综合样例_键盘处理_cities.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>provinces</key>
<array>
<string>安徽</string>
<string>澳门</string>
<string>北京</string>
<string>重庆</string>
<string>福建</string>
<string>甘肃</string>
<string>广东</string>
<string>广西</string>
<string>贵州</string>
<string>海南</string>
<string>河北</string>
<string>河南</string>
<string>黑龙江</string>
<string>湖北</string>
<string>湖南</string>
<string>吉林</string>
<string>江苏</string>
<string>江西</string>
<string>辽宁</string>
<string>内蒙古</string>
<string>宁夏</string>
<string>青海</string>
<string>山东</string>
<string>山西</string>
<string>陕西</string>
<string>上海</string>
<string>四川</string>
<string>台湾</string>
<string>天津</string>
<string>西藏</string>
<string>香港</string>
<string>新疆</string>
<string>云南</string>
<string>浙江</string>
</array>
<key>cities</key>
<dict>
<key>北京</key>
<array>
<string>东城区</string>
<string>西城区</string>
<string>崇文区</string>
<string>宣武区</string>
<string>海淀区</string>
<string>朝阳区</string>
<string>丰台区</string>
<string>石景山区</string>
<string>通州区</string>
<string>顺义区</string>
<string>房山区</string>
<string>大兴区</string>
<string>昌平区</string>
<string>怀柔区</string>
<string>平谷区</string>
<string>门头沟区</string>
<string>密云县</string>
<string>延庆县</string>
</array>
<key>重庆</key>
<array>
<string>重庆</string>
</array>
<key>上海</key>
<array>
<string>上海</string>
</array>
<key>天津</key>
<array>
<string>天津</string>
</array>
<key>安徽</key>
<array>
<string>合肥</string>
<string>安庆</string>
<string>蚌埠</string>
<string>亳州</string>
<string>巢湖</string>
<string>池州</string>
<string>滁州</string>
<string>阜阳</string>
<string>淮北</string>
<string>淮南</string>
<string>黄山</string>
<string>黄山景区</string>
<string>九华山景区</string>
<string>六安</string>
<string>马鞍山</string>
<string>青阳</string>
<string>宿州</string>
<string>铜陵</string>
<string>芜湖</string>
<string>宣城</string>
</array>
<key>福建</key>
<array>
<string>福州</string>
<string>龙岩</string>
<string>南平</string>
<string>宁德</string>
<string>莆田</string>
<string>泉州</string>
<string>三明</string>
<string>厦门</string>
<string>永安</string>
<string>漳州</string>
</array>
<key>广东</key>
<array>
<string>广州</string>
<string>潮州</string>
<string>从化</string>
<string>东莞</string>
<string>佛山</string>
<string>河源</string>
<string>鹤山</string>
<string>化州</string>
<string>惠州</string>
<string>江门</string>
<string>揭阳</string>
<string>茂名</string>
<string>梅州</string>
<string>清远</string>
<string>汕头</string>
<string>汕尾</string>
<string>韶关</string>
<string>深圳</string>
<string>阳江</string>
<string>云浮</string>
<string>湛江</string>
<string>肇庆</string>
<string>中山</string>
<string>珠海</string>
</array>
<key>甘肃</key>
<array>
<string>兰州</string>
<string>白银</string>
<string>定西</string>
<string>甘南</string>
<string>嘉峪关</string>
<string>酒泉</string>
<string>临夏</string>
<string>陇南</string>
<string>平凉</string>
<string>庆阳</string>
<string>天水</string>
<string>武威</string>
<string>张掖</string>
</array>
<key>广西</key>
<array>
<string>南宁</string>
<string>百色</string>
<string>北海</string>
<string>北流</string>
<string>崇左</string>
<string>防城港</string>
<string>贵港</string>
<string>桂林</string>
<string>桂平</string>
<string>河池</string>
<string>贺州</string>
<string>来宾</string>
<string>柳州</string>
<string>钦州</string>
<string>梧州</string>
<string>宜州</string>
<string>玉林</string>
</array>
<key>贵州</key>
<array>
<string>贵阳</string>
<string>安顺</string>
<string>毕节</string>
<string>都匀</string>
<string>凯里</string>
<string>六盘水</string>
<string>铜仁</string>
<string>兴义</string>
<string>遵义</string>
</array>
<key>河北</key>
<array>
<string>石家庄</string>
<string>保定</string>
<string>泊头</string>
<string>沧州</string>
<string>承德</string>
<string>邯郸</string>
<string>河间</string>
<string>衡水</string>
<string>廊坊</string>
<string>秦皇岛</string>
<string>任丘</string>
<string>唐山</string>
<string>邢台</string>
<string>张家口</string>
</array>
<key>河南</key>
<array>
<string>郑州</string>
<string>安阳</string>
<string>鹤壁</string>
<string>济源</string>
<string>焦作</string>
<string>开封</string>
<string>洛阳</string>
<string>漯河</string>
<string>南阳</string>
<string>平顶山</string>
<string>濮阳</string>
<string>三门峡</string>
<string>商丘</string>
<string>新乡</string>
<string>信阳</string>
<string>许昌</string>
<string>周口</string>
<string>驻马店</string>
</array>
<key>黑龙江</key>
<array>
<string>哈尔滨</string>
<string>大庆</string>
<string>大兴安岭</string>
<string>鹤岗</string>
<string>黑河</string>
<string>虎林</string>
<string>鸡西</string>
<string>佳木斯</string>
<string>密山</string>
<string>牡丹江</string>
<string>宁安</string>
<string>七台河</string>
<string>齐齐哈尔</string>
<string>双鸭山</string>
<string>绥化</string>
<string>五常</string>
<string>伊春</string>
</array>
<key>湖北</key>
<array>
<string>武汉</string>
<string>鄂州</string>
<string>恩施</string>
<string>黄冈</string>
<string>黄石</string>
<string>荆门</string>
<string>荆州</string>
<string>潜江</string>
<string>十堰</string>
<string>随州</string>
<string>天门</string>
<string>仙桃</string>
<string>咸宁</string>
<string>襄樊</string>
<string>孝感</string>
<string>宜昌</string>
</array>
<key>湖南</key>
<array>
<string>长沙</string>
<string>常德</string>
<string>郴州</string>
<string>衡阳</string>
<string>怀化</string>
<string>吉首</string>
<string>耒阳</string>
<string>冷水江</string>
<string>娄底</string>
<string>韶山</string>
<string>邵阳</string>
<string>湘潭</string>
<string>湘乡</string>
<string>益阳</string>
<string>永州</string>
<string>岳阳</string>
<string>张家界</string>
<string>株州</string>
</array>
<key>吉林</key>
<array>
<string>长春</string>
<string>白城</string>
<string>白山</string>
<string>珲春</string>
<string>吉林</string>
<string>辽源</string>
<string>龙井</string>
<string>舒兰</string>
<string>四平</string>
<string>松原</string>
<string>通化</string>
<string>延边</string>
</array>
<key>江苏</key>
<array>
<string>南京</string>
<string>常州</string>
<string>高邮</string>
<string>淮安</string>
<string>连云港</string>
<string>南通</string>
<string>苏州</string>
<string>宿迁</string>
<string>太仓</string>
<string>泰州</string>
<string>无锡</string>
<string>新沂</string>
<string>徐州</string>
<string>盐城</string>
<string>扬州</string>
<string>镇江</string>
</array>
<key>江西</key>
<array>
<string>南昌</string>
<string>抚州</string>
<string>赣州</string>
<string>吉安</string>
<string>景德镇</string>
<string>九江</string>
<string>萍乡</string>
<string>上饶</string>
<string>新余</string>
<string>宜春</string>
<string>鹰潭</string>
</array>
<key>辽宁</key>
<array>
<string>沈阳</string>
<string>鞍山</string>
<string>本溪</string>
<string>朝阳</string>
<string>大连</string>
<string>丹东</string>
<string>抚顺</string>
<string>阜新</string>
<string>葫芦岛</string>
<string>锦州</string>
<string>辽阳</string>
<string>盘锦</string>
<string>铁岭</string>
<string>营口</string>
</array>
<key>内蒙古</key>
<array>
<string>呼和浩特</string>
<string>阿拉善盟</string>
<string>巴彦淖尔盟</string>
<string>包头</string>
<string>赤峰</string>
<string>鄂尔多斯</string>
<string>呼伦贝尔</string>
<string>通辽</string>
<string>乌海</string>
<string>乌兰察布盟</string>
<string>锡林郭勒盟</string>
<string>兴安盟</string>
</array>
<key>宁夏</key>
<array>
<string>银川</string>
<string>固原</string>
<string>石嘴山</string>
<string>吴忠</string>
<string>中卫</string>
</array>
<key>青海</key>
<array>
<string>西宁</string>
<string>果洛</string>
<string>海北</string>
<string>海东</string>
<string>海南</string>
<string>海西</string>
<string>黄南</string>
<string>玉树</string>
</array>
<key>四川</key>
<array>
<string>成都</string>
<string>阿坝</string>
<string>巴中</string>
<string>崇州</string>
<string>达州</string>
<string>大邑</string>
<string>德阳</string>
<string>都江堰</string>
<string>峨眉山</string>
<string>甘孜</string>
<string>广安</string>
<string>广元</string>
<string>江油</string>
<string>金堂</string>
<string>乐山</string>
<string>泸州</string>
<string>眉山</string>
<string>绵阳</string>
<string>内江</string>
<string>南充</string>
<string>攀枝花</string>
<string>遂宁</string>
<string>西昌</string>
<string>雅安</string>
<string>宜宾</string>
<string>资阳</string>
<string>自贡</string>
</array>
<key>山东</key>
<array>
<string>济南</string>
<string>滨州</string>
<string>德州</string>
<string>东营</string>
<string>肥城</string>
<string>海阳</string>
<string>菏泽</string>
<string>济宁</string>
<string>莱芜</string>
<string>莱阳</string>
<string>聊城</string>
<string>临沂</string>
<string>平度</string>
<string>青岛</string>
<string>青州</string>
<string>日照</string>
<string>泰安</string>
<string>威海</string>
<string>潍坊</string>
<string>烟台</string>
<string>枣庄</string>
<string>章丘</string>
<string>淄博</string>
</array>
<key>陕西</key>
<array>
<string>西安</string>
<string>安康</string>
<string>宝鸡</string>
<string>汉中</string>
<string>商洛</string>
<string>铜川</string>
<string>渭南</string>
<string>咸阳</string>
<string>兴平</string>
<string>延安</string>
<string>榆林</string>
</array>
<key>山西</key>
<array>
<string>太原</string>
<string>长治</string>
<string>大同</string>
<string>晋城</string>
<string>晋中</string>
<string>临汾</string>
<string>吕梁</string>
<string>朔州</string>
<string>忻州</string>
<string>阳泉</string>
<string>运城</string>
</array>
<key>新疆</key>
<array>
<string>乌鲁木齐</string>
<string>阿克苏</string>
<string>阿拉尔</string>
<string>阿图什</string>
<string>博乐</string>
<string>昌吉</string>
<string>哈密</string>
<string>和田</string>
<string>喀什</string>
<string>克拉玛依</string>
<string>库尔勒</string>
<string>石河子</string>
<string>图木舒克</string>
<string>吐鲁番</string>
<string>五家渠</string>
<string>伊宁</string>
</array>
<key>西藏</key>
<array>
<string>拉萨</string>
<string>阿里</string>
<string>昌都</string>
<string>林芝</string>
<string>那曲</string>
<string>日喀则</string>
<string>山南</string>
</array>
<key>云南</key>
<array>
<string>昆明</string>
<string>保山</string>
<string>楚雄</string>
<string>大理</string>
<string>德宏</string>
<string>迪庆</string>
<string>个旧</string>
<string>丽江</string>
<string>临沧</string>
<string>怒江</string>
<string>曲靖</string>
<string>思茅</string>
<string>文山</string>
<string>西双版纳</string>
<string>玉溪</string>
<string>昭通</string>
</array>
<key>浙江</key>
<array>
<string>杭州</string>
<string>北仑</string>
<string>慈溪</string>
<string>奉化</string>
<string>湖州</string>
<string>嘉兴</string>
<string>金华</string>
<string>丽水</string>
<string>临海</string>
<string>宁波</string>
<string>宁海</string>
<string>衢州</string>
<string>三门</string>
<string>绍兴</string>
<string>台州</string>
<string>天台</string>
<string>温岭</string>
<string>温州</string>
<string>仙居</string>
<string>象山</string>
<string>义乌</string>
<string>余姚</string>
<string>舟山</string>
</array>
<key>澳门</key>
<array>
<string>澳门</string>
</array>
<key>台湾</key>
<array>
<string>台北</string>
<string>高雄</string>
<string>台南</string>
<string>台中</string>
</array>
<key>香港</key>
<array>
<string>香港</string>
</array>
<key>海南</key>
<array>
<string>海口</string>
<string>儋州</string>
<string>东方</string>
<string>琼海</string>
<string>三亚</string>
<string>万宁</string>
<string>文昌</string>
<string>五指山</string>
</array>
</dict>
</dict>
</plist>
H:/0712/综合样例_键盘处理_KeyboardTool.h
// KeyboardTool.h
// 动画和事件综合样例-键盘处理
// Created by mj on 13-4-16.
// Copyright (c) 2013年 itcast. All rights reserved.
#import <UIKit/UIKit.h>
// 声明协议
@protocol KeyboardToolDelegate;
// 定义枚举类型 KeyboardToolButtonType
typedef enum {
kKeyboardToolButtonTypeNext, // 说明是点击:下一个button
kKeyboardToolButtonTypePrevious, // 说明是点击:上一个button
kKeyboardToolButtonTypeDone // 说明是点击:完毕button
} KeyboardToolButtonType;
@interface KeyboardTool : UIToolbar
// 连线用的,button,各自是下一个,上一个,完毕button
@property (nonatomic, readonly) IBOutlet UIBarButtonItem *nextBtn;
@property (nonatomic, readonly) IBOutlet UIBarButtonItem *previousBtn;
@property (nonatomic, readonly) IBOutlet UIBarButtonItem *doneBtn;
// 代理对象_toolDelegate,必须遵守协议,一般用assign策略
@property (nonatomic, weak) id<KeyboardToolDelegate> toolDelegate;
+ (id)keyboardTool;
// 这里写成 - 是为了能在xib中连线
- (IBAction)next;
- (IBAction)previous;
- (IBAction)done;
@end
// 协议中定义详细的方法,參数1是当前的KeyboardTool对象,參数2是枚举类型
@protocol KeyboardToolDelegate <NSObject>
- (void)keyboardTool:(KeyboardTool *)tool
buttonClick:(KeyboardToolButtonType)type;
@end
H:/0712/综合样例_键盘处理_KeyboardTool.m
// KeyboardTool.m
// 动画和事件综合样例-键盘处理
// Created by mj on 13-4-16.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "KeyboardTool.h"
@implementation KeyboardTool
#pragma mark 类方法,从xib文件里初始化一个KeyboardTool对象
+ (id)keyboardTool {
// owner能够传KeyboardTool这个类
// 点击"下一个"按钮的时候,要调用owner的next方法
NSArray *array = [[NSBundle mainBundle]
loadNibNamed:@"keyboardTool" owner:nil options:nil];
// 返回初始化完毕的KeyboardTool对象
return [array lastObject];
} #pragma mark - 响应按钮点击,分别相应下一个,上一个,完毕按钮
- (void)next {
// 假设代理_toolDelegate,实现了方法
if ([_toolDelegate respondsToSelector:@selector(keyboardTool:buttonClick:)]) {
// 那么就调用代理的方法,而且传參数进去
// self是当前对象KeyboardTool,点击类型是定义好了的枚举
[_toolDelegate keyboardTool:self buttonClick:kKeyboardToolButtonTypeNext];
}
}
- (void)previous {
// 假设代理_toolDelegate,实现了方法
if ([_toolDelegate respondsToSelector:@selector(keyboardTool:buttonClick:)]) {
// 那么就调用代理的方法,而且传參数进去
// self是当前对象KeyboardTool,点击类型是定义好了的枚举
[_toolDelegate keyboardTool:self buttonClick:kKeyboardToolButtonTypePrevious];
}
}
- (void)done {
// 假设代理_toolDelegate,实现了方法
if ([_toolDelegate respondsToSelector:@selector(keyboardTool:buttonClick:)]) {
// 那么就调用代理的方法,而且传參数进去
// self是当前对象KeyboardTool,点击类型是定义好了的枚举
[_toolDelegate keyboardTool:self buttonClick:kKeyboardToolButtonTypeDone];
}
}
@end
H:/0712/综合样例_键盘处理_MJViewController.h
// MJViewController.h
// 键盘处理
// Created by mj on 13-7-9.
// Copyright (c) 2013年 itcast. All rights reserved.
#import <UIKit/UIKit.h>
#import "KeyboardTool.h" @interface MJViewController : UIViewController
<UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate,
KeyboardToolDelegate>
// 生日输入框
@property (weak, nonatomic) IBOutlet UITextField *birthdayView;
// 城市输入框
@property (weak, nonatomic) IBOutlet UITextField *cityView; @end
H:/0712/综合样例_键盘处理_MJViewController.m
// MJViewController.m
// 键盘处理
// Created by mj on 13-7-9.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "MJViewController.h"
@interface MJViewController ()
// 数组,成员是省份名字
@property (nonatomic, strong) NSArray *allProvinces;
// 字典,键是省名,值是数组,是由城市名字组成的数组
@property (nonatomic, strong) NSDictionary *allCities;
// 活动的输入框
@property (nonatomic, weak) UITextField *activeTextField;
// 输入框上面的工具条
@property (nonatomic, weak) KeyboardTool *tool;
// 数组,成员是全部的输入框控件
@property (nonatomic, strong) NSMutableArray *allTextFields;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化数组,用于记录全部的输入框控件
self.allTextFields = [NSMutableArray array];
// 通过类方法,生成键盘工具条
self.tool = [KeyboardTool keyboardTool];
// 设置键盘工具条的代理对象是:当前控制器
self.tool.toolDelegate = self;
// 设置全部文本框的键盘工具条
for (UITextField *field in self.view.subviews)
{
// 假设不是输入框控件,跳过~
if (![field isKindOfClass:[UITextField class]]) continue;
// 设置键盘的上方附属品为KeyboardTool
field.inputAccessoryView = self.tool;
// 同一时候,用数组记录下,全部的输入框控件
[self.allTextFields addObject:field];
// 设置输入框的代理对象为当前控制器
field.delegate = self;
}
// 设置生日的键盘UIDatePicker(不用设置宽高和位置)
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
// 设置区域为中国中文简体
datePicker.locale = [[NSLocale alloc]
initWithLocaleIdentifier:@"zh_CN"];
// 仅仅显示日期
datePicker.datePickerMode = UIDatePickerModeDate;
// 监听datePicker的改变
[datePicker addTarget:self action:@selector(dateChange:)
forControlEvents:UIControlEventValueChanged];
// 生日输入框的键盘就是UIDatePicker
self.birthdayView.inputView = datePicker;
// 设置城市的键盘UIPickerView
UIPickerView *picker = [[UIPickerView alloc] init];
// UIPickerView的数据源是当前控制器
picker.dataSource = self;
// 高亮显示当前选中的UIPickerView的一行
picker.showsSelectionIndicator = YES;
// UIPickerView的代理也是当前控制器
picker.delegate = self;
// 城市输入框的键盘就是UIPickerView
self.cityView.inputView = picker;
// 载入省份数据字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"cities.plist"
ofType:nil]];
// 第1列显示全部的省名,能够从数组allProvinces中取
self.allProvinces = dict[@"provinces"];
// 第2列显示省以下的全部的城市名,能够从字典allCities中取
self.allCities = dict[@"cities"];
}
#pragma mark 日期选择控件UIDatePicker的值改变了
- (void)dateChange:(UIDatePicker *)picker
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd";
// 设置生日输入框的内容为转换后的日期选择器的日期
self.birthdayView.text = [formatter stringFromDate:picker.date];
}
#pragma mark UIPickerView数据源方法,一共多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
#pragma mark UIPickerView数据源方法,每一列相应多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return self.allProvinces.count;
} else {
// 获取省份的位置
NSUInteger pIndex = [pickerView selectedRowInComponent:0];
// 省名数组中,取出省名
NSString *pName = self.allProvinces[pIndex];
// 依据KEY省名,找到相应的城市名组成的数组
NSArray *cities = self.allCities[pName];
return cities.count;
}
}
#pragma mark UIPickerView代理方法,每列每行显示什么数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
// 显示省名
return self.allProvinces[row];
} else {
// 获取省份的位置
NSUInteger pIndex = [pickerView selectedRowInComponent:0];
// 获取省名
NSString *pName = self.allProvinces[pIndex];
// 获取省名相应的城市数组
NSArray *cities = self.allCities[pName];
return cities[row];
}
}
#pragma mark UIPickerView代理方法,选中了某一行就会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// 重要-----为产生联动效果,需手动刷新第1列的数据
[pickerView reloadComponent:1];
// selectedRowInComponent获得第0列的当前行号
NSUInteger pIndex = [pickerView selectedRowInComponent:0];
NSString *pName = self.allProvinces[pIndex];
// selectedRowInComponent获得第1列的当前行号
NSUInteger cIndex = [pickerView selectedRowInComponent:1];
NSArray *cities = self.allCities[pName];
NSString *cName = cities[cIndex];
// 设置城市输入框里面的文本
self.cityView.text = [NSString stringWithFormat:@"%@ %@", pName, cName];
}
#pragma mark - UITextField代理方法,文本框能否够返回
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// 退出全部的键盘,即结束输入
[self.view endEditing:YES];
return YES;
}
#pragma mark - UITextField代理方法,一開始编辑的时候就会调用
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// 找到当前输入框在总的输入框数组中的位置,即索引
NSUInteger index = [self.allTextFields indexOfObject:textField];
// 依据索引index,决定是否激活上一个button
self.tool.previousBtn.enabled = index != 0;
// 依据索引index,决定是否激活下一个button
self.tool.nextBtn.enabled = index != self.allTextFields.count - 1;
// 而且记录当前被激活的文本框是哪个,以下的方法中要用到
self.activeTextField = textField;
}
#pragma mark - Keytool代理
- (void)keyboardTool:(KeyboardTool *)tool
buttonClick:(KeyboardToolButtonType)type
{
if (type == kKeyboardToolButtonTypeDone) {
// 假设是点击KeyTool上的完毕button,退出键盘就可以
[self.activeTextField resignFirstResponder];
} else {
// 得到当前激活的输入框在全部输入框组成的数组中的位置
NSUInteger index = [self.allTextFields indexOfObject:self.activeTextField];
if (type == kKeyboardToolButtonTypePrevious) {
// 假设是点击KeyTool上的上一个button
index--;
} else {
// 假设是点击KeyTool上的下一个button
index++;
}
// 得到相应的那一个输入框控件,并成为第一响应者,弹出键盘
UITextField *field = self.allTextFields[index];
[field becomeFirstResponder];
}
} @end
IOS_DatePicker_PickerView_SegmentControl_键盘处理的更多相关文章
- HTML kbd键盘元素
1. 说明 kbd :即Keyboard Input Element(键盘输入元素).表示键盘按键的语义元素,常用于网页上对快捷键.按键说明的场景. 样式规格:内联样式. 为了在页面上突出显示,可以给 ...
- MVVM TextBox的键盘事件
MVVM下RichTextBox的键盘回车事件设置为发送,不是回车 xmlns:i="http://schemas.microsoft.com/expression/2010/interac ...
- android键盘
在应用的开发过程中有不少的情况下会用到自定义键盘,例如支付宝的支付密码的输入,以及类似的场景.android系统给开发者们提供了系统键盘,KeyboardView,其实并不复杂,只是有些开发者不知道罢 ...
- Android如何制作漂亮的自适布局的键盘
最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思路. 这里最上面的titlebar高度固定,下面输入的金额高 ...
- iOS 键盘添加完成按钮,delegate和block回调
这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...
- WPF 捕获键盘输入事件
最近修改的一个需求要求捕获键盘输入的 Text,包括各种标点符号. 最开始想到的是 PreviewKeyDown 或者 PreviewKeyUp 这样的键盘事件. 但是这两个事件的对象 KeyEven ...
- [转载]从MyEclipse到IntelliJ IDEA-让你摆脱鼠标,全键盘操作
从MyEclipse转战到IntelliJ IDEA的经历 注转载址:http://blog.csdn.net/luoweifu/article/details/13985835 我一个朋友写了一篇“ ...
- html中键盘事件----在路上(16)
键盘事件,这里以onkeyup为例: 解析:当在一个input中输入文本时,在另一个div中输出文本 在下面是本人写的小demo,供分享. 代码如下: <!DOCTYPE html> &l ...
- Chrome DevTools – 键盘和UI快捷键参考
Chrome DevTools有几个内置的快捷键,可以节省你的日常工作的时间. 本指南提供了Chrome DevTools中每个快捷键的快速参考.虽然一些快捷方式在全局范围内可用,但其他的快捷方式用于 ...
随机推荐
- maya 2014帮助手册中 三维概念讲解
maya 2014 帮助手册中 三维概念讲解 多边形简介 三个或更多的边, 顶点 边 面 组成 经常使用三边形或四边形来建模 n边形不常用 单个多边形称为面 多个面连接到 ...
- Python包和日志模块
1.什么是包 包是模块的一种形式,包的本质就是一个含有__init__.py文件的文件夹 2.为什么要有包 提高开发人员维护性 3.如何用包 导入包就是在导包下的__init__.py ...
- Wannafly挑战赛6
完全平方数 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 131072K,其他语言262144K64bit IO Format: %lld 题目描述 多次查询[l,r]范围内的完全平方 ...
- JQuery常用的HTML页控制取值、赋值
1,关于tab页签 获取当前页签的属性: var tabsSelect=$("#easytabs").tabs("getSelected"); var titl ...
- 九度oj 题目1349:数字在排序数组中出现的次数
题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数组的大小.1<=n <= 10^6. 第二行有n个整数,表示数组元素,每个元素均 ...
- php单一入口和多入口模式详细讲解
php单一入口模式可谓是现在一种比较流行的大型web应用开发模式,比如当前比较流行的一些php开发框架,zend,thinkphp,qeephp,还有cakephp 等他们都是采用的单一入口模式的.本 ...
- ApplicationContext,WebApplicationContext
servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象. ApplicationContext,WebApplicationContext 是Spring的Bea ...
- springboot中的controller注解没有生效
springboot中的controller注解没有生效 , 启动的Application类没有在controller的父目录或同级目录
- 眉目传情之匠心独运的kfifo【转】
转自:http://blog.csdn.net/chen19870707/article/details/39899743 权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 一 ...
- 监听EditText输入完成
最近有个需求,要在用户输入完快递单号之后,请求快递100接口,拿到快递公司信息.总不能用户输入一个数字就请求一次吧,给服务器造成不必要的压力(虽然不是自家服务器).但是又无法知晓用户何时输入完毕,每家 ...