UIPickView的使用(菜单系统)


UI进阶-第01

/**

摘要:

掌握UIPicderView的使用

掌握键盘工具条的使用

*/

 

一、UIPikcerView的使用

》打开官方文档 查看DatePicker/UIPikcerView在iOS6与iOS7的区别

查找至UserExperience— Guides — iOS 7 UI Transition Guide — Controls - Picker

 

"【案例:点菜系统】"

//放个点菜系统的截图

 

》使用UIPikcerView控件实现点菜系统

》掌握UIPikcerView的代理与数据源与代理方法的使用,与TableView类比

(1)UITableView的每一行Cell是在数据源里,而UIPikcerView的每一行View是在代理里

(2)UIPickerView每一行长什么样有两个方法

//-(NSString*)pickerView: titleForRow: forComponent:直接返回一个字符串

//-(UIView*)pickerView: viewForRow: forComponent: reusingView:直接返回一个view

 

》掌握使用代理的【-(NSString *)pickerView:titleForRow:forComponent:】方法显示一组数据与显示多组数据

 

》加载foods.plist文件,显示多组数据

 

》监听每组选中的行,更改Label数据

(1)使用代理方法【-(void)pickerView:didSelectRow:inComponent:】

 

》实现默认选中每一组的第一行数据

(1)在viewDidLoad方法调用【-(void)pickerView:didSelectRow:inComponent:】实现

 

》实现随机选菜单

(1)实现Label数据的随机变更

(2)实现pickerView的数据随机变更

(3)每一组对应行的数据一定要不同上一次的行数据

/**

*获取旧行与新行,使用while循环,

    //旧行

    NSInteger oldRow = [self.pickerViewselectedRowInComponent:i];

    //随机新行

    NSInteger newRow =arc4random_uniform((int)rows);

    //新行与旧行相同,再随机,直到不两只

    while (newRow == oldRow) {

        newRow = arc4random_uniform((int)rows);

    }

*/

 

 

 

 

UI进阶-第01

/**

摘要:

掌握UIPicderView的使用

掌握键盘工具条的使用

*/

 

一、UIPikcerView的使用

》打开官方文档 查看DatePicker/UIPikcerView在iOS6与iOS7的区别

查找至UserExperience— Guides — iOS 7 UI Transition Guide — Controls - Picker

 

"【案例:点菜系统】"

//放个点菜系统的截图

 

》使用UIPikcerView控件实现点菜系统

》掌握UIPikcerView的代理与数据源与代理方法的使用,与TableView类比

(1)UITableView的每一行Cell是在数据源里,而UIPikcerView的每一行View是在代理里

(2)UIPickerView每一行长什么样有两个方法

//-(NSString*)pickerView: titleForRow: forComponent:直接返回一个字符串

//-(UIView*)pickerView: viewForRow: forComponent: reusingView:直接返回一个view

 

》掌握使用代理的【-(NSString *)pickerView:titleForRow:forComponent:】方法显示一组数据与显示多组数据

 

》加载foods.plist文件,显示多组数据

 

》监听每组选中的行,更改Label数据

(1)使用代理方法【-(void)pickerView:didSelectRow:inComponent:】

 

》实现默认选中每一组的第一行数据

(1)在viewDidLoad方法调用【-(void)pickerView:didSelectRow:inComponent:】实现

 

》实现随机选菜单

(1)实现Label数据的随机变更

(2)实现pickerView的数据随机变更

(3)每一组对应行的数据一定要不同上一次的行数据

/**

*获取旧行与新行,使用while循环,

    //旧行

    NSInteger oldRow = [self.pickerViewselectedRowInComponent:i];

    //随机新行

    NSInteger newRow =arc4random_uniform((int)rows);

    //新行与旧行相同,再随机,直到不两只

    while (newRow == oldRow) {

        newRow = arc4random_uniform((int)rows);

    }

*/

 

 

 

 ViewController.m

//  01.点菜系统

//

//  Created by Yong Feng Guo on 14-12-16.

//  Copyright (c) 2014 Fung. All rights reserved.

//

 

#import"ViewController.h"

 

@interface ViewController()<UIPickerViewDataSource,UIPickerViewDelegate>

 

@property(nonatomic,strong)NSArray *foods;

 

@property (weak,nonatomic) IBOutlet UILabel *fruitLabel;

@property (weak,nonatomic) IBOutlet UILabel *mainFoodLabel;

@property (weak,nonatomic) IBOutlet UILabel *drinkLabel;

@property (weak,nonatomic) IBOutlet UIPickerView *pickerView;

 

@end

 

@implementation ViewController

 

/**

 *懒加载食物数据

 */

-(NSArray*)foods{

    if(!_foods) {

        NSString *foodsPath = [[NSBundlemainBundle] pathForResource:@"foods.plist" ofType:nil];

        _foods = [NSArrayarrayWithContentsOfFile:foodsPath];

    }

   

    return _foods;

}

 

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typicallyfrom a nib.

    //默认显示每一组的第一行数据

    NSInteger components = self.foods.count;

    for(NSInteger i = 0; i<components; i++) {

        [self pickerView:nil didSelectRow:0 inComponent:i];

    }

}

 

#pragmamark -UIPickerView数据原

#pragmamark 多少组

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return self.foods.count;

}

 

#pragmamark 每组多少行

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

 

    NSArray *items = self.foods[component];

    return items.count;

}

 

#pragmamark -UIPickerView数据代理

#pragmamark 对应组对应行的数据

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    NSArray *items = self.foods[component];

    return items[row];

}

 

 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component{

   

    //获取对应组对应行的数据

    NSString *food = self.foods[component][row];

    switch (component) {

        case 0:

            self.fruitLabel.text = food;

            break;

        case 1:

            self.mainFoodLabel.text = food;

            break;

        case 2:

            self.drinkLabel.text = food;

            break;

        default:

            break;

    }

}

 

#pragmamark 随机菜单

- (IBAction)randomMenu:(id)sender {

  

   

    NSInteger component = self.foods.count;

   

    //生成每一组的随机数据

    for(NSInteger i = 0; i < component; i++) {

        NSArray *items = self.foods[i];

        NSInteger rows = items.count;

       

        //旧行

        NSInteger oldRow = [self.pickerView selectedRowInComponent:i];

       

        //随机新行

        NSInteger newRow = arc4random_uniform((int)rows);

       

        //新行与旧行相同,再随机,直到不两只

        while (newRow == oldRow) {

            newRow = arc4random_uniform((int)rows);

        }

       

        //pickerView没有变

        [self pickerView:nildidSelectRow:newRow inComponent:i];

       

        //改变pickerViewCell

        [self.pickerView selectRow:newRow inComponent:i animated:YES];

    }

   

}

 

 

@end

 

"【出题】"

1>生成0.0-0.9的小数  arc4random_uniform(10) *0.1)

2>生成0.00-0.99的小数 arc4random_uniform(100) *0.01)

用乘效率会高些。

 

 

"【案例:国旗选择】"

//此处插入国旗选择的图片

》加载flags.plist数据到 "国旗模型[Flag.h]"

》掌握代理【-(UIView*)pickerView:viewForRow:forComponent:reusingView:】的使用

(1)在数据源里返回一组数据,行数由国旗个数决定

(2)在代理方法中使用上面的方法,每一行返回一个View,返回的这个view为label

(3)打印reusingView的地址和文字,"查看循环利用的view" --'备课的时候多演示几次'

//eg:NSLog(@"==%p %@",label,label.text);

(4)使用一个xib描述国家和国旗

(5)掌握一个设置行高的代理方法

 

 

"【案例:省市联动】"

》添加Province模型加载plist文件

》实现PickerView的数据源和代理

(1)返回两组数据

(2)实现PickerView的代理,使用【-(UIView*)pickerView:viewForRow:forComponent:reusingView:】方法,返回一个Label

(3)添加一个当前省份选中索引IndexOfProv

(4)默认城市那一组数据 返回 第一个省份的城市数据

(5)当前省份选中的索引改变时,刷新城市数据并默认选中的第一个城市

*[pickerViewreloadComponent:1];

*[pickerViewselectRow:0 inComponent:1 animated:YES];

 

》掌握设置组宽的代理方法

(1)实现【-(CGFloat)pickerView:widthForComponent:】方法设置组的宽度

(2)设置两列View的背景颜色不同看出宽度的区别

 

 

二、DatePicker的使用

"【案例: DatePicker&键盘处理】"

》掌握DatePicker本地化的设置

(1)storyboard设置本地化

(2)获取系统可以的本地化[NSLocale availableLocaleIdentifiers]

(3)代码设置日期的本地化

 

》掌握DataPikcer的日期格式设置

(1)storybard设置日期格式

(2)代码设置日期格式 datePickerMode属性

 

》掌握UITextField如果弹出日期选择器并在键盘添加工具条

(1)设置UITextField的inputView属性为日期选择器即可

(2)熟悉UIToolbar的使用,添加UIBarButtonItem

(3)自定义一个键盘工具条,添加在键盘上,设置UITextField的inputAccessoryView属性

(4)通过代理监听键盘工具条的几个按钮

 

》熟悉代码实现UIToolBar

":

"1.创建ToolBar要设置frm

"2.添加固定弹簧时,一定要设置宽度

 

 

 

 

 

 

 ViewController.m

//  01.点菜系统

//

//  Created by Yong Feng Guo on 14-12-16.

//  Copyright (c) 2014 Fung. All rights reserved.

//

 

#import"ViewController.h"

 

@interface ViewController()<UIPickerViewDataSource,UIPickerViewDelegate>

 

@property(nonatomic,strong)NSArray *foods;

 

@property (weak,nonatomic) IBOutlet UILabel *fruitLabel;

@property (weak,nonatomic) IBOutlet UILabel *mainFoodLabel;

@property (weak,nonatomic) IBOutlet UILabel *drinkLabel;

@property (weak,nonatomic) IBOutlet UIPickerView *pickerView;

 

@end

 

@implementation ViewController

 

/**

 *懒加载食物数据

 */

-(NSArray*)foods{

    if(!_foods) {

        NSString *foodsPath = [[NSBundlemainBundle] pathForResource:@"foods.plist" ofType:nil];

        _foods = [NSArrayarrayWithContentsOfFile:foodsPath];

    }

   

    return _foods;

}

 

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typicallyfrom a nib.

    //默认显示每一组的第一行数据

    NSInteger components = self.foods.count;

    for(NSInteger i = 0; i<components; i++) {

        [self pickerView:nil didSelectRow:0 inComponent:i];

    }

}

 

#pragmamark -UIPickerView数据原

#pragmamark 多少组

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return self.foods.count;

}

 

#pragmamark 每组多少行

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

 

    NSArray *items = self.foods[component];

    return items.count;

}

 

#pragmamark -UIPickerView数据代理

#pragmamark 对应组对应行的数据

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    NSArray *items = self.foods[component];

    return items[row];

}

 

 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component{

   

    //获取对应组对应行的数据

    NSString *food = self.foods[component][row];

    switch (component) {

        case 0:

            self.fruitLabel.text = food;

            break;

        case 1:

            self.mainFoodLabel.text = food;

            break;

        case 2:

            self.drinkLabel.text = food;

            break;

        default:

            break;

    }

}

 

#pragmamark 随机菜单

- (IBAction)randomMenu:(id)sender {

  

   

    NSInteger component = self.foods.count;

   

    //生成每一组的随机数据

    for(NSInteger i = 0; i < component; i++) {

        NSArray *items = self.foods[i];

        NSInteger rows = items.count;

       

        //旧行

        NSInteger oldRow = [self.pickerView selectedRowInComponent:i];

       

        //随机新行

        NSInteger newRow = arc4random_uniform((int)rows);

       

        //新行与旧行相同,再随机,直到不两只

        while (newRow == oldRow) {

            newRow = arc4random_uniform((int)rows);

        }

       

        //pickerView没有变

        [self pickerView:nildidSelectRow:newRow inComponent:i];

       

        //改变pickerViewCell

        [self.pickerView selectRow:newRow inComponent:i animated:YES];

    }

   

}

 

 

@end

 

"【出题】"

1>生成0.0-0.9的小数  arc4random_uniform(10) *0.1)

2>生成0.00-0.99的小数 arc4random_uniform(100) *0.01)

用乘效率会高些。

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值