IOS開發(3)之UIPickerView控制項

來源:互聯網
上載者:User

1 前言
UIPickerView(選取器)也是出鏡率相當高的控制項,類似與HTML中的Select,今天我們就一起來學習一下這個控制項。

2 UIPickerView簡介

關鍵代碼:

.h檔案:


[plain]
#import <UIKit/UIKit.h> 
 
//需要實現UIPickerViewDataSource,UIPickerViewDelegate協議 
@interface ZYViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> 
 
@property(nonatomic,strong) UIPickerView *myPicker; 
 
@end 

#import <UIKit/UIKit.h>

//需要實現UIPickerViewDataSource,UIPickerViewDelegate協議
@interface ZYViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,strong) UIPickerView *myPicker;

@end.m檔案:


[plain]
@implementation ZYViewController 
 
@synthesize myPicker; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
     
    self.myPicker = [[UIPickerView alloc] init]; 
    self.myPicker.dataSource = self; 
    self.myPicker.center = self.view.center; 
     
    self.myPicker.delegate = self; 
    //設定選取器的水平的陰影製作效果 
    self.myPicker.showsSelectionIndicator = YES; 
     
    [self.view addSubview:self.myPicker]; 

 
 
// returns the number of 'columns' to display. 
//為選取器添加組建,如果需要多個組建,可以修改該方法體 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    NSInteger result = 0; 
    if([pickerView isEqual:self.myPicker]){ 
        result = 1; 
    } 
    return result; 

 
// returns the # of rows in each component.. 
//定義一個組件中有多少個選項,如果想為想象添加值,需要修改該方法體 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    NSInteger result = 0; 
    if ([pickerView isEqual:self.myPicker]) { 
        result = 10; 
    } 
    return result; 

 
//UIPickerViewDelegate 
//設定UIPickerView的每個組件的標題 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 
    NSString *result = nil; 
    if ([pickerView isEqual:self.myPicker]) { 
        result = [NSString stringWithFormat:@"Row %ld",(long)row+1]; 
    } 
    return result; 

 
//選擇時候觸發的事件 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

    NSLog(@"result: You selected Row%ld",(long)row+1); 

@implementation ZYViewController

@synthesize myPicker;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
   
    self.myPicker = [[UIPickerView alloc] init];
    self.myPicker.dataSource = self;
    self.myPicker.center = self.view.center;
   
    self.myPicker.delegate = self;
    //設定選取器的水平的陰影製作效果
    self.myPicker.showsSelectionIndicator = YES;
   
    [self.view addSubview:self.myPicker];
}


// returns the number of 'columns' to display.
//為選取器添加組建,如果需要多個組建,可以修改該方法體
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    NSInteger result = 0;
    if([pickerView isEqual:self.myPicker]){
        result = 1;
    }
    return result;
}

// returns the # of rows in each component..
//定義一個組件中有多少個選項,如果想為想象添加值,需要修改該方法體
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSInteger result = 0;
    if ([pickerView isEqual:self.myPicker]) {
        result = 10;
    }
    return result;
}

//UIPickerViewDelegate
//設定UIPickerView的每個組件的標題
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *result = nil;
    if ([pickerView isEqual:self.myPicker]) {
        result = [NSString stringWithFormat:@"Row %ld",(long)row+1];
    }
    return result;
}

//選擇時候觸發的事件
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"result: You selected Row%ld",(long)row+1);
}
運行結果:

 
 


當選擇某個選項時候控制台運行結果:


2013-04-22 15:27:21.163 UIPickerViewTest[649:c07] result: You selected Row1

2013-04-22 15:31:49.433 UIPickerViewTest[649:c07] result: You selected Row2

2013-04-22 15:31:51.327 UIPickerViewTest[649:c07] result: You selected Row4

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.