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.监听添加按钮事件
4.监听删除按钮事件
---------------------------------
动态添加一行:
1.首先创建一行UIView
2.把创建的一行,添加到控制器的UIView中去
*/
/*
1.添加行,每一行有对应的子控件
2.删除行
*/
#import <UIKit/UIKit.h>
@interface MJViewController : UIViewController
// UIBarButtonItem 删除 storyBoard里面将其enabled复选框去掉
@property (nonatomic, weak) IBOutlet UIBarButtonItem *removeItem;
// 响应按钮点击,添加一行
- (IBAction)addRow;
// 响应按钮点击,删除一行
- (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.监听添加按钮事件
4.监听删除按钮事件
---------------------------------
动态添加一行:
1.首先创建一行UIView
2.把创建的一行,添加到控制器的UIView中去
*/
#import "MJViewController.h"
// 常量:行高
#define kRowHeight 70
@interface MJViewController ()
@end
@implementation MJViewController
#pragma mark 响应按钮点击,添加一行
- (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];
// 设置按钮的背景图片
[iconView setBackgroundImage:image forState:UIControlStateNormal];
[newRow addSubview:iconView];
}
#pragma mark 响应按钮点击,删除一行
- (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.监听图片按钮点击
4.添加和删除的动画效果
*/
#import <UIKit/UIKit.h>
@interface MJViewController : UIViewController
// UIBarButtonItem 删除 storyBoard里面将其enabled复选框去掉
@property (nonatomic, weak) IBOutlet UIBarButtonItem *removeItem;
// 响应按钮点击,添加一行
- (IBAction)addRow;
// 响应按钮点击,删除一行
- (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 响应按钮点击,添加一行
- (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];
// 设置按钮的背景图片
[iconView setBackgroundImage:image forState:UIControlStateNormal];
// 绑定按钮的监听器
[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 监听并响应按钮图标的点击事件
- (void)iconClick:(UIButton *)icon
{
// 1.获取父控件
UIView *superview = icon.superview;
// 2.获取姓名标签
UILabel *nameView = (UILabel *)[superview viewWithTag:kNameTag];
NSLog(@"名称是----%@", nameView.text);
}
#pragma mark 响应按钮点击,删除一行
- (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中的按钮添加点击事件,标准步骤:
1,首先XIB中的,FILE'S OWNER选择为MJViewController
2,[bundle loadNibNamed:@"row" owner:self options:nil];
owner参数传入当前控制器的实例对象
3,在MJViewController.h头文件中准备一个方法IBAction,
并且,与XIB中的按钮完成拖线
这样,当点击XIB中的按钮时,将调用FILE'S OWNER类,的实例对象的已经连线的IBAction方法
*/
// 响应图标按钮点击,弹出AlertView
- (IBAction)iconClick:(UIButton *)sender;
// 响应按钮点击,添加新的一行
- (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中的按钮添加点击事件,标准步骤:
1,首先XIB中的,FILE'S OWNER选择为MJViewController
2,[bundle loadNibNamed:@"row" owner:self options:nil];
owner参数传入当前控制器的实例对象
3,在MJViewController.h头文件中准备一个方法IBAction,
并且,与XIB中的按钮完成拖线
这样,当点击XIB中的按钮时,将调用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;
// 响应设置按钮的点击,作用是从底部弹出设置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"]];
}
// 响应设置按钮的点击,作用是从底部弹出设置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, // 说明是点击:下一个按钮
kKeyboardToolButtonTypePrevious, // 说明是点击:上一个按钮
kKeyboardToolButtonTypeDone // 说明是点击:完成按钮
} KeyboardToolButtonType;
@interface KeyboardTool : UIToolbar
// 连线用的,按钮,分别是下一个,上一个,完成按钮
@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,决定是否激活上一个按钮
self.tool.previousBtn.enabled = index != 0;
// 根据索引index,决定是否激活下一个按钮
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上的完成按钮,退出键盘即可
[self.activeTextField resignFirstResponder];
} else {
// 得到当前激活的输入框在所有输入框组成的数组中的位置
NSUInteger index = [self.allTextFields indexOfObject:self.activeTextField];
if (type == kKeyboardToolButtonTypePrevious) {
// 如果是点击KeyTool上的上一个按钮
index--;
} else {
// 如果是点击KeyTool上的下一个按钮
index++;
}
// 得到对应的那一个输入框控件,并成为第一响应者,弹出键盘
UITextField *field = self.allTextFields[index];
[field becomeFirstResponder];
}
}
@end