您的位置:首页 > 产品设计 > UI/UE

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__);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  初级iOS iOS