UIPickerView选择器笔记
2015-11-22 14:50
471 查看
1蹲守代理
<UIPickerViewDataSource,
UIPickerViewDelegate>
//设置数据源和代理
self.pickerView.dataSource
=
self;
self.pickerView.delegate =
self;
2.
#pragma mark
代理方法
1. 返回每行的内容
- (NSString
*)pickerView:(UIPickerView
*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return
@"itcast";
}
2. 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView
*)pickerView
{
return
3;
}
3. 返回某列有多少行
- (NSInteger)pickerView:(UIPickerView
*)pickerView numberOfRowsInComponent:(NSInteger)component
{
return
5;
二.点菜系统案例
1.当手指点击选择器松开的时候调用
2.单数组的懒加载
3.随机点击事件.随机点菜
//随机点击事件
- (IBAction)randomClick:(id)sender
{
//arc4random_uniform(3)
取值范围
0 ~ n-1
//for循环随机显示
for (int
i =
0; i <
self.foods.count;
i++)
{
//获取第一列的个数
int count = (int)[self.foods[i]
count];
//获取随机的行数
int row =
arc4random_uniform(count);
//0 ~ count -1
//获取当前的行数
NSInteger
index = [self.pickerView
selectedRowInComponent:i];
// if (row == index)
// {
// row = arc4random_uniform(count);
// }
do {
row =
arc4random_uniform(count);
}
while
(row == index);
//选中某列某行
[self.pickerView
selectRow:row
inComponent:i
animated:YES];
//手动调用didselected
[self
pickerView:nil
didSelectRow:row
inComponent:i];
}
4.选择国旗的案例
字典转模型
#import
<Foundation/Foundation.h>
@interface
CZFlag :
NSObject
@property
(nonatomic,strong)
NSString
* name;
@property
(nonatomic,strong)
NSString
* icon;
+ (instancetype) flagWithDict:(NSDictionary
*) dict;
@end
#import
"CZFlag.h"
@implementation
CZFlag
+ (instancetype) flagWithDict:(NSDictionary
*) dict
{
CZFlag
* flag = [[self
alloc]
init];//更具有扩展性
[flag
setValuesForKeysWithDictionary:dict];
return
flag;
}
@end
5.返回每行视图的方法
//返回每行视图
- (UIView
*)pickerView:(UIPickerView
*)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView
*)view
{
//iOS7之前
iOS7 iOS8 view一直为空
<
1350f
br />// if(view == nil)
// {
//
// }
//获取数据模型
CZFlag
* f =
self.flags[row];
CZFlagView
* flagView = [CZFlagView
flagView];
//赋值
flagView.flag
= f;
return flagView;
}
6.城市选择的案例
//数组越界
reason: '-[__NSCFArray objectAtIndex:]: index (4) beyond bounds (1)
- (void)pickerView:(UIPickerView
*)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (component ==
0)
{
self.selectedIndex
= row;
// [pickerView reloadAllComponents];//刷新所有的列
[pickerView
reloadComponent:1];//刷新指定列
//第二列默认选中第一个城市
[pickerView
selectRow:0
inComponent:1
animated:YES];
}
NSLog(@"didSelectRow");
}
@end
7.工具条
8.项目中的文件
10.PCH文件
1.创建PCH文件
2.设置PCH文件
11.UIApplicaicationg
//一个工程中只有一个UIApplication
单例
//不能alloc
+ init 会报错
12,点击屏幕的方法,会调用百度
- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//获取app
UIApplication
* app = [UIApplication
sharedApplication];
// app.statusBarHidden = YES;
//带动画的隐藏
// [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
NSString
* urlString =
@"http://www.baidu.com";
//创建URL
NSURL
* url = [NSURL
URLWithString:urlString];
[app
openURL:url];
}
13.是否隐藏状态栏
//是否隐藏状态栏
//- (BOOL) prefersStatusBarHidden
//{
// return NO;
//}
////更改状态栏的样式
//- (UIStatusBarStyle) preferredStatusBarStyle
//{ 白色的
// return UIStatusBarStyleLightContent;
//}
14.获取APP
//获取app
UIApplication
* app = [UIApplication
sharedApplication];
15获取APP并且设置徽章
//获取app
UIApplication
* app = [UIApplication
sharedApplication];
//根据系统版本
获取用户权限
if ([[[UIDevice
currentDevice]
systemVersion]
floatValue] >=
8)
{
UIUserNotificationSettings
*settings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeBadge
categories:nil];
[app
registerUserNotificationSettings:settings];
}
//设置徽章
// app.applicationIconBadgeNumber = 11;
app.applicationIconBadgeNumber
=
0;
16AppDelegata的各种方法说明
1.当程序启动完毕的时候,调用此方法
//当程序启动完毕
调用此方法
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
NSLog(@"%s",__func__);
return
YES;
}
2.app将要吃去获得状态 (失去焦点 不能和用户交互)
//app将要辞去活动状态
(失去焦点
不能和用户交互)
- (void)applicationWillResignActive:(UIApplication
*)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the
background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
NSLog(@"%s",__func__);
}
3.APP 进入后台的时候调用
//app进入后台时
调用
- (void)applicationDidEnterBackground:(UIApplication
*)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"%s",__func__);
}
4.app 将要进入前台(恢复之前的状态)
//app将要进入前台
(恢复之前的状态)
- (void)applicationWillEnterForeground:(UIApplication
*)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog(@"%s",__func__);
}
5.app进入获得专题 (重新获取焦点 可以喝用户交互)
//app进入活动状态
(重新获取焦点
可以和用户交互)
- (void)applicationDidBecomeActive:(UIApplication
*)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSLog(@"%s",__func__);
}
6.app杀死应用程序
//app杀死应用程序
- (void)applicationWillTerminate:(UIApplication
*)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"%s",__func__);
}
7.app接受内存警告
//app接受到内存警告
- (void)applicationDidReceiveMemoryWarning:(UIApplication
*)application
{
NSLog(@"%s",__func__);
}
<UIPickerViewDataSource,
UIPickerViewDelegate>
//设置数据源和代理
self.pickerView.dataSource
=
self;
self.pickerView.delegate =
self;
2.
#pragma mark
代理方法
1. 返回每行的内容
- (NSString
*)pickerView:(UIPickerView
*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return
@"itcast";
}
2. 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView
*)pickerView
{
return
3;
}
3. 返回某列有多少行
- (NSInteger)pickerView:(UIPickerView
*)pickerView numberOfRowsInComponent:(NSInteger)component
{
return
5;
二.点菜系统案例
1.当手指点击选择器松开的时候调用
2.单数组的懒加载
3.随机点击事件.随机点菜
//随机点击事件
- (IBAction)randomClick:(id)sender
{
//arc4random_uniform(3)
取值范围
0 ~ n-1
//for循环随机显示
for (int
i =
0; i <
self.foods.count;
i++)
{
//获取第一列的个数
int count = (int)[self.foods[i]
count];
//获取随机的行数
int row =
arc4random_uniform(count);
//0 ~ count -1
//获取当前的行数
NSInteger
index = [self.pickerView
selectedRowInComponent:i];
// if (row == index)
// {
// row = arc4random_uniform(count);
// }
do {
row =
arc4random_uniform(count);
}
while
(row == index);
//选中某列某行
[self.pickerView
selectRow:row
inComponent:i
animated:YES];
//手动调用didselected
[self
pickerView:nil
didSelectRow:row
inComponent:i];
}
4.选择国旗的案例
字典转模型
#import
<Foundation/Foundation.h>
@interface
CZFlag :
NSObject
@property
(nonatomic,strong)
NSString
* name;
@property
(nonatomic,strong)
NSString
* icon;
+ (instancetype) flagWithDict:(NSDictionary
*) dict;
@end
#import
"CZFlag.h"
@implementation
CZFlag
+ (instancetype) flagWithDict:(NSDictionary
*) dict
{
CZFlag
* flag = [[self
alloc]
init];//更具有扩展性
[flag
setValuesForKeysWithDictionary:dict];
return
flag;
}
@end
5.返回每行视图的方法
//返回每行视图
- (UIView
*)pickerView:(UIPickerView
*)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView
*)view
{
//iOS7之前
iOS7 iOS8 view一直为空
<
1350f
br />// if(view == nil)
// {
//
// }
//获取数据模型
CZFlag
* f =
self.flags[row];
CZFlagView
* flagView = [CZFlagView
flagView];
//赋值
flagView.flag
= f;
return flagView;
}
6.城市选择的案例
//数组越界
reason: '-[__NSCFArray objectAtIndex:]: index (4) beyond bounds (1)
- (void)pickerView:(UIPickerView
*)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (component ==
0)
{
self.selectedIndex
= row;
// [pickerView reloadAllComponents];//刷新所有的列
[pickerView
reloadComponent:1];//刷新指定列
//第二列默认选中第一个城市
[pickerView
selectRow:0
inComponent:1
animated:YES];
}
NSLog(@"didSelectRow");
}
@end
7.工具条
8.项目中的文件
10.PCH文件
1.创建PCH文件
2.设置PCH文件
11.UIApplicaicationg
//一个工程中只有一个UIApplication
单例
//不能alloc
+ init 会报错
12,点击屏幕的方法,会调用百度
- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//获取app
UIApplication
* app = [UIApplication
sharedApplication];
// app.statusBarHidden = YES;
//带动画的隐藏
// [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
NSString
* urlString =
@"http://www.baidu.com";
//创建URL
NSURL
* url = [NSURL
URLWithString:urlString];
[app
openURL:url];
}
13.是否隐藏状态栏
//是否隐藏状态栏
//- (BOOL) prefersStatusBarHidden
//{
// return NO;
//}
////更改状态栏的样式
//- (UIStatusBarStyle) preferredStatusBarStyle
//{ 白色的
// return UIStatusBarStyleLightContent;
//}
14.获取APP
//获取app
UIApplication
* app = [UIApplication
sharedApplication];
15获取APP并且设置徽章
//获取app
UIApplication
* app = [UIApplication
sharedApplication];
//根据系统版本
获取用户权限
if ([[[UIDevice
currentDevice]
systemVersion]
floatValue] >=
8)
{
UIUserNotificationSettings
*settings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeBadge
categories:nil];
[app
registerUserNotificationSettings:settings];
}
//设置徽章
// app.applicationIconBadgeNumber = 11;
app.applicationIconBadgeNumber
=
0;
16AppDelegata的各种方法说明
1.当程序启动完毕的时候,调用此方法
//当程序启动完毕
调用此方法
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
NSLog(@"%s",__func__);
return
YES;
}
2.app将要吃去获得状态 (失去焦点 不能和用户交互)
//app将要辞去活动状态
(失去焦点
不能和用户交互)
- (void)applicationWillResignActive:(UIApplication
*)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the
background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
NSLog(@"%s",__func__);
}
3.APP 进入后台的时候调用
//app进入后台时
调用
- (void)applicationDidEnterBackground:(UIApplication
*)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"%s",__func__);
}
4.app 将要进入前台(恢复之前的状态)
//app将要进入前台
(恢复之前的状态)
- (void)applicationWillEnterForeground:(UIApplication
*)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog(@"%s",__func__);
}
5.app进入获得专题 (重新获取焦点 可以喝用户交互)
//app进入活动状态
(重新获取焦点
可以和用户交互)
- (void)applicationDidBecomeActive:(UIApplication
*)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSLog(@"%s",__func__);
}
6.app杀死应用程序
//app杀死应用程序
- (void)applicationWillTerminate:(UIApplication
*)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"%s",__func__);
}
7.app接受内存警告
//app接受到内存警告
- (void)applicationDidReceiveMemoryWarning:(UIApplication
*)application
{
NSLog(@"%s",__func__);
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器