UI:UIPickerView 拾取視圖

簡介

  • UIPickerView 是一個選擇器控件, 它可以生成單列的選擇器,也可生成多列的選擇器,而且開發(fā)者完全可以自定義選擇項的外觀庄撮,因此用法非常靈活。 UIPickerView 直接繼承了 UIView 毙籽,沒有繼承 UIControl 洞斯,因此,它不能像 UIControl 那樣綁定事件處理方法坑赡, UIPickerView 的事件處理由其委托對象完成烙如。
  • 蘋果官方解釋:
    The UIPickerView class implements objects, called picker views, that use a spinning-wheel or slot-machine metaphor to show one or more sets of values. Users select values by rotating the wheels so that the desired row of values aligns with a selection indicator.

初始化

- (instancetype)initWithFrame:(CGRect)frame;

常用屬性

  • dataSource:設置數(shù)據(jù)源
  • delegate:設置代理
  • showsSelectionIndicator:該屬性控制是否顯示UIPickerView中的選中標記(以高亮背景作為選中標記)。
  • numberOfComponents: 獲取UIPickerView指定列中包含的列表項的數(shù)量毅否。該屬性是一個只讀屬性亚铁。

常用方法

// 1、刷新所有列 - (void)reloadAllComponents; 
// 2螟加、刷新指定列 - (void)reloadComponent:(NSInteger)component;

UIPickerViewDataSource

  • 數(shù)據(jù)源徘溢,UIPickerViewDataSource為一個協(xié)議,該協(xié)議提供了如下方法配置UIPickerView的列數(shù)與行數(shù)仰迁。
// 1甸昏、設置列數(shù) 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// 2顽分、設置行數(shù)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

UIPickerViewDelegate

  • 代理徐许,同樣的,UIPickerViewDelegate也為一個協(xié)議卒蘸,該協(xié)議可監(jiān)聽用戶交互雌隅,選中某行數(shù)據(jù),亦可配置UIPickerView的行高及列寬等缸沃。
// 1恰起、設置列寬
 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

// 2、設置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

// 3趾牧、設置每行顯示標題
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

// 4检盼、自定義行視圖
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;

// 5、選擇某行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

效果展示

20160119210749628.gif

代碼實現(xiàn)

  • 案例分析:從上面效果當點擊北京所在的視圖的時候翘单,從屏幕下方彈出了拾取視圖吨枉,是不是有點熟悉呢蹦渣?沒錯,這似乎類似于文本輸入框貌亭,當文本輸入框在響應用戶交互的時候柬唯,對應的鍵盤會彈起。那我們就按照這樣的思路去實現(xiàn)上述效果圃庭。首先锄奢,我們需自定義鍵盤,通過設置文本輸入框的inputView屬性剧腻,將其設值為pickerView即可拘央,那么【確定】按鈕又如何實現(xiàn)呢?其實很簡單书在,對應的我們可設置文本輸入框的附件視圖屬性inputAccessoryView堪滨,解決了這個問題之后,可能還有疑問蕊温,那就是文本輸入框的光標如何解決呢袱箱?不用擔心,我們可設置文本輸入框的tintColor屬性义矛,設值為透明色clearColor即可发笔。解決了幾個主要的問題,那么要實現(xiàn)上述效果就變得簡單了凉翻,下面我將直接貼上源碼了讨,供各位參考,不到之處制轰,還望指點前计。
#import "ViewController.h" 

#define RGB_COLOR(_R,_G,_B,_A) [UIColor colorWithRed:_R/255.0 green:_G/255.0 blue:_B/255.0 alpha:_A] 

@interface ViewController () <UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSArray *_dataSource; /**< 數(shù)據(jù)源 */ NSInteger _index;
}

@property (nonatomic, strong) UIPickerView *pickerView;/**< 拾取器 */
@property (nonatomic, strong) UITextField  *textField; /**< 文本輸入框 */

- (void)initializeDataSource; /**< 初始化數(shù)據(jù)源 */
- (void)initializeUserInterface; /**< 初始化用戶界面 */

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeDataSource];
    [self initializeUserInterface];
}


#pragma mark *** Initialize methods ***

- (void)initializeDataSource {
    // 初始化數(shù)據(jù)源
    _dataSource = @[@"北京", @"上海", @"成都", @"上海", @"深圳"];
}

- (void)initializeUserInterface {
    // 文本標簽
    UILabel *titleLabel = [[UILabel alloc] init];
    
    titleLabel.bounds = CGRectMake(0, 0, 60, 30);
    titleLabel.center = CGPointMake(80, 200);
    titleLabel.text = @"城市:";
    titleLabel.font = [UIFont boldSystemFontOfSize:25];
    
    [self.view addSubview:titleLabel];
    
    // 加載文本輸入框
    [self.view addSubview:self.textField];
    
    // 自定義鍵盤樣式
    self.textField.inputView = self.pickerView;
    
    UIButton *sureButton = [[UIButton alloc] init];
    
    sureButton.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 30);
    sureButton.center = CGPointMake(CGRectGetMidX(self.view.bounds), 25);
    sureButton.backgroundColor = RGB_COLOR(231, 231, 231, 1);
    
    // 設置標題對齊方式請1
    sureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    [sureButton setTitle:@"【確 定】" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [sureButton addTarget:self action:@selector(respondsToSureButton:) forControlEvents:UIControlEventTouchUpInside];
    // 自定義鍵盤附件視圖
    _textField.inputAccessoryView = sureButton;
}


#pragma mark *** Events ***

- (void)respondsToSureButton:(UIButton *)sender {
    
    // 收起鍵盤,失去第一響應
    [_textField resignFirstResponder];
    
    _textField.text = _dataSource[_index];
}


#pragma mark ***  <UIPickerViewDelegate, UIPickerViewDataSource> ***
// 設置列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// 設置行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _dataSource.count;
}

// 設置標題
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return _dataSource[row];
}

// 點擊某行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _index = row;
}

// 自定義行
/*
 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
 return nil;
 }
 */


#pragma mark *** Touches ***

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


#pragma mark *** Getters ***

- (UITextField *)textField {
    if (!_textField) {
        // 文本輸入框
        _textField = [[UITextField alloc] init];
        _textField.bounds = CGRectMake(0, 0, 220, 30);
        _textField.center = CGPointMake(220, 200);
        _textField.borderStyle = UITextBorderStyleBezel;
        _textField.textAlignment = NSTextAlignmentCenter;
        // 修改光標顏色
        _textField.tintColor = [UIColor clearColor];
        _textField.text = @"北京";
    }
    return _textField;
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 300, 220)];
        _pickerView.backgroundColor = RGB_COLOR(244, 244, 244, 1);
        // 設置代理
        _pickerView.delegate = self;
        // 設置數(shù)據(jù)源
        _pickerView.dataSource = self;
        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
}

@end
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末垃杖,一起剝皮案震驚了整個濱河市男杈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌调俘,老刑警劉巖伶棒,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異彩库,居然都是意外死亡肤无,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門骇钦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宛渐,“玉大人,你說我怎么就攤上這事】妫” “怎么了畴蹭?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鳍烁。 經(jīng)常有香客問我叨襟,道長,這世上最難降的妖魔是什么幔荒? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任糊闽,我火速辦了婚禮,結果婚禮上爹梁,老公的妹妹穿的比我還像新娘右犹。我一直安慰自己,他們只是感情好姚垃,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布念链。 她就那樣靜靜地躺著,像睡著了一般积糯。 火紅的嫁衣襯著肌膚如雪掂墓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天看成,我揣著相機與錄音君编,去河邊找鬼。 笑死川慌,一個胖子當著我的面吹牛吃嘿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播梦重,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼兑燥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了琴拧?” 一聲冷哼從身側響起降瞳,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎艾蓝,沒想到半個月后力崇,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體斗塘,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡赢织,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了馍盟。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片于置。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出八毯,到底是詐尸還是另有隱情搓侄,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布话速,位于F島的核電站讶踪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏泊交。R本人自食惡果不足惜乳讥,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望廓俭。 院中可真熱鬧云石,春花似錦、人聲如沸研乒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽雹熬。三九已至宽菜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間竿报,已是汗流浹背赋焕。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仰楚,地道東北人隆判。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像僧界,于是被迫代替她去往敵國和親侨嘀。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容