UIPickerView透過一個滾軸讓使用者選擇項目,

此範例為有data1 to data3假資料,

使用者選擇後點擊按鈕顯示至lableView上。

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSArray *dataArray1;
}
@end

viewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labelShow;
- (IBAction)toPick:(id)sender;
@property (weak, nonatomic) IBOutlet UIPickerView *myPickerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    dataArray1 = [NSArray arrayWithObjects:@"data1", @"data2",@"data3", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)toPick:(id)sender
{
    _labelShow.text = [dataArray1 objectAtIndex:[_myPickerView selectedRowInComponent:0]];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [dataArray1 objectAtIndex:row];
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [dataArray1 count];
}
@end

 

arrow
arrow
    文章標籤
    UIPickerView
    全站熱搜
    創作者介紹
    創作者 Will 的頭像
    Will

    Will的部落格

    Will 發表在 痞客邦 留言(0) 人氣()