1.UIPickerView的寬度和高度是固定的,縱向是320216,橫向是568162
2.屬性:
@property(nonatomic,readonly)NSInteger numberOfComponents; // 選擇框的行數
@property(nonatomic,assign)idUIPickerViewDataSource> dataSource; (相似於UITableView)
@property(nonatomic,assign)idUIPickerViewDelegate>delegate; (相似於UITableView)
(BOOL)showsSelectionIndicator// 能否顯示選擇指導器 ,等於一個藍色的條
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
// 指定Delegate
pickerView.delegate=self;
// 顯示選中框
pickerView.showsSelectionIndicator=YES;
[self.view addSubview:pickerView];
以上可以在視圖顯示一個拔取器,然則內容空白,pickerView.showsSelectionIndicator=YES;是這只以後拔取器所選中的內容:
拔取器上顯示數據,必需依附兩個協定,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView *pickerView;
NSArray *pickerData;
}
@end
3.然後在.m文件的ViewDidLoad中初始化界面
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
// 指定Delegate
pickerView.delegate=self;
// 顯示選中框
pickerView.showsSelectionIndicator=YES;
[self.view addSubview:pickerView];
NSArray *dataArray = [[NSArray alloc]initWithObjects:@"許嵩",@"周傑倫",@"梁靜茹",@"許飛",@"鳳凰傳奇",@"阿杜",@"方年夜同",@"林豪傑",@"胡夏",@"邱永傳", nil];
pickerData=dataArray;
// 添加按鈕
CGRect frame = CGRectMake(120, 250, 80, 40);
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectButton.frame=frame;
[selectButton setTitle:@"SELECT" forState:UIControlStateNormal];
[selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectButton];
}
4.完成UIPickerView的署理辦法,將數據顯示在拔取器上所須要幾個辦法
#pragma mark -
#pragma mark Picker Date Source Methods
//前往顯示的列數
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//前往以後列顯示的行數
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
//前往以後行的內容,此處是將數組中數值添加到轉動的誰人顯示欄上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
前兩個是數據源的署理辦法,一個是前往列,有幾個拔取器就前往幾,第二個是設置拔取器有若干行,由於就這一個拔取器,所以直接前往行數,即數組元素個數若干;第三個署理辦法是將數組元素添加到了拔取器下面顯示;
說一下兩個協定實例辦法
UIPickerViewDelegate中的實例辦法
// 當用戶選擇某個row時
- (void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
// 當其在繪制row內容,須要row的高度時
(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
// 前往指定component.row顯示的文本
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
// 當picker view須要給指定的component.row指定view時,挪用此函數.前往值為用作row內容的view
(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
// row的寬度
(CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
UIPickerViewDataSource中的實例辦法
依照官方文檔的說法,UIPickerViewDataSource這個協定唯一的功效就是供給picker view中component的個數和各個component中的row的個數,固然名為datasource,然則它任務於MVC的C中
本協定唯一兩個實例辦法,均須要完成:
// 前往列數
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
// 前往每列對應的行數
(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
5.關於按鈕呼應事宜,關於按鈕的構成和添加呼應事宜不再提,後面都有,
(void) buttonPressed:(id)sender
{
NSInteger row =[pickerView selectedRoWinComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:@"你選擇的是:%@",selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒"
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
@UIPickerView還有其他實例辦法
// 獲得指定列的行數
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
// 刷新一切的列
(void) reloadAllComponents
// 刷新指定的列
(void) reloadComponent: (NSInteger) component
(CGSize) rowSizeForComponent: (NSInteger) component
// 獲得某列選擇的行數
(NSInteger) selectedRoWinComponent: (NSInteger) component
// 選擇一行
(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
PS:多個component對應分歧title的辦法
有時刻我們須要有多個component的UIPickerView而且對應分歧的內容,好比地域的選擇,須要有省分和城市兩個選項,選擇分歧的省分,城市要響應產生變更。
上面假定component數目為2。
應用指定title的函數,依據[pickerView selectedRoWinComponent:0]的分歧來指定第二個component的title
- (NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
}
但此時,會發明切換省分後,城市一欄沒有方法實時刷新。
我們還要指定刷新事宜。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[pickerView reloadComponent:1];
}
【詳解iOS App中UIPickerView轉動選擇欄的添加辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!