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)

用乘效率會高些。

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章