日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

timerpickerview使用_详解iOS App中UIPickerView滚动选择栏的添加方法

發(fā)布時(shí)間:2025/3/12 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 timerpickerview使用_详解iOS App中UIPickerView滚动选择栏的添加方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.UIPickerView的寬度和高度是固定的,縱向是320216,橫向是568162

2.屬性:

@property(nonatomic,readonly)NSInteger numberOfComponents; // 選擇框的行數(shù)

@property(nonatomic,assign)idUIPickerViewDataSource> dataSource; (類似于UITableView)

@property(nonatomic,assign)idUIPickerViewDelegate>delegate; (類似于UITableView)

(BOOL)showsSelectionIndicator// 是否顯示選擇指示器 ,即是一個(gè)藍(lán)色的條

pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];

//??? 指定Delegate

pickerView.delegate=self;

//??? 顯示選中框

pickerView.showsSelectionIndicator=YES;

[self.view addSubview:pickerView];

以上可以在視圖顯示一個(gè)選取器,但是內(nèi)容空白,pickerView.showsSelectionIndicator=YES;是這只當(dāng)前選取器所選中的內(nèi)容:

選取器上顯示數(shù)據(jù),必須依賴兩個(gè)協(xié)議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中

#import

@interface ViewController : UIViewController

{

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.實(shí)現(xiàn)UIPickerView的代理方法,將數(shù)據(jù)顯示在選取器上所需要幾個(gè)方法

#pragma mark -

#pragma mark Picker Date Source Methods

//返回顯示的列數(shù)

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 1;

}

//返回當(dāng)前列顯示的行數(shù)

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return [pickerData count];

}

#pragma mark Picker Delegate Methods

//返回當(dāng)前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到滾動(dòng)的那個(gè)顯示欄上

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return [pickerData objectAtIndex:row];

}

前兩個(gè)是數(shù)據(jù)源的代理方法,一個(gè)是返回列,有幾個(gè)選取器就返回幾,第二個(gè)是設(shè)置選取器有多少行,因?yàn)榫瓦@一個(gè)選取器,所以直接返回行數(shù),即數(shù)組元素個(gè)數(shù)多少;第三個(gè)代理方法是將數(shù)組元素添加到了選取器上面顯示;

說一下兩個(gè)協(xié)議實(shí)例方法

UIPickerViewDelegate中的實(shí)例方法

// 當(dāng)用戶選擇某個(gè)row時(shí)

- (void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:?????????????????????????????? (NSInteger)component

// 當(dāng)其在繪制row內(nèi)容,需要row的高度時(shí)

(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component

// 返回指定component.row顯示的文本

(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component

// 當(dāng)picker view需要給指定的component.row指定view時(shí),調(diào)用此函數(shù).返回值為用作row內(nèi)容的view

(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view

// row的寬度

(CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component

UIPickerViewDataSource中的實(shí)例方法

按照官方文檔的說法,UIPickerViewDataSource這個(gè)協(xié)議僅有的功能就是提供picker view中component的個(gè)數(shù)和各個(gè)component中的row的個(gè)數(shù),雖然名為datasource,但是它工作于MVC的C中

本協(xié)議僅有兩個(gè)實(shí)例方法,均需要實(shí)現(xiàn):

// 返回列數(shù)

(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

// 返回每一列對(duì)應(yīng)的行數(shù)

(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component

5.關(guān)于按鈕響應(yīng)事件,關(guān)于按鈕的形成和添加響應(yīng)事件不再提,前面都有,

(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還有其他實(shí)例方法

// 獲取指定列的行數(shù)

- (NSInteger) numberOfRowsInComponent:(NSInteger)component

// 刷新所有的列

(void) reloadAllComponents

// 刷新指定的列

(void) reloadComponent: (NSInteger) component

(CGSize) rowSizeForComponent: (NSInteger) component

// 獲取某列選擇的行數(shù)

(NSInteger) selectedRowInComponent: (NSInteger) component

// 選擇一行

(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated

(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component

PS:多個(gè)component對(duì)應(yīng)不同title的方法有時(shí)候我們需要有多個(gè)component的UIPickerView并且對(duì)應(yīng)不同的內(nèi)容,比如地區(qū)的選擇,需要有省份和城市兩個(gè)選項(xiàng),選擇不同的省份,城市要相應(yīng)發(fā)生變化。

下面假設(shè)component數(shù)量為2。

使用指定title的函數(shù),根據(jù)[pickerView selectedRowInComponent:0]的不同來指定第二個(gè)component的title

- (NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

}

但此時(shí),會(huì)發(fā)現(xiàn)切換省份后,城市一欄沒有辦法及時(shí)刷新。

我們還要指定刷新事件。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

[pickerView reloadComponent:1];

}

總結(jié)

以上是生活随笔為你收集整理的timerpickerview使用_详解iOS App中UIPickerView滚动选择栏的添加方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。