UIPickerView 選擇器

UIPickerView 繼承了UIView 沒有繼承UIControl? UIPickerView的時間處理由其委托對象完成

使用Component標明列 Row標明列中單個元素

numberOfCompinents:獲取UIPickerView 指定列中包含的列表項的數(shù)量。該屬性只讀

showsSelectionIndicator: 是否顯示UIPV中的選中標記(以高亮背景作為選中標記)

numberOfRowsInComponent: 獲取UIPV包含的列數(shù)量

rowSizeForCompinent:獲取UIPV包含的指定列中列表項的大小。該方法返回一個CGSize對象

selectRow:inComponent:animated:該方法設(shè)置選中該UIPV中指定列的特定列表項 最后一個 是否使用動畫

selectRowInCompinent:返回該UIPickerView指定列中悲酸中的列表項

viewForRow:forComponent:返回該UIPV指定列的列表項所使用的UIView控件

UIDatePicker只是負責該空間的通用行為,而該空間包含多少列,格列包含多少個列表項則有UIPickerViewDataSource對象負責

開發(fā)者必須weiUIPickerView設(shè)置UIPickerViewDataSource對象,并實現(xiàn)兩種方法:

numberOFComponentsInPickerView:該UIPickerView將通過該方法來判斷應(yīng)該包含多少列

pickerView:numBerOfRowsInCompont:判斷指定列包含多少個列表項

UIPickerView需要控制各列額寬度,以及各列中列表項的大小和外觀,或程序需要為UIPicker的選中事件提供相應(yīng),都需要為UIPickerView設(shè)置UIPVDelegate委托對象,并根據(jù)需要實現(xiàn)該委托對象中的如下方法

-pickerView:rowHeightForCompinent:該方法返回的CGFloatz值將作為該UIPV控件中指定列中列表項的高度

-pickerView:widthForComponent:寬

-~titleForRow;forComponent:指定列的列表項的文本標題

~viewForRow:forComponent:reusingView:返回的UIPV控件中指定列的指定列表項

UIPV-InterfaceBuilder -ShowsSelectionIndicator 是否顯示UIPV中選中標記(高亮背景)

10.12.1 單列選擇器

- (void)viewDidLoad {

[super viewDidLoad];

self.books=[NSArray arrayWithObjects:@"第一項",@"第二項",@"第三項", nil];

self.pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 20, 400, 500)];

self.pickerView.dataSource=self;

self.pickerView.delegate=self;

[self.view addSubview:self.pickerView];

// Do any additional setup after loading the view, typically from a nib.

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 1;

}

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

return self.books.count;

}

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

//根據(jù)row參數(shù)返回books中的元素,row參數(shù)代表列表項的編號

//因此該方法表示第幾個列表項,就使用books中的第幾個元素

return [self.books objectAtIndex:row];

}

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

//使用一個UIAlertView來系那是用戶選中的列表項

UIAlertView* alert=[[UIAlertView alloc]

initWithTitle:@"提示" message:[NSString stringWithFormat: @"你選中的圖書是:%@",[self.books objectAtIndex:row]] delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];

[alert show];

}

10.12.2多項選擇器

- (void)viewDidLoad {

[super viewDidLoad];

_initState=0;

self.books=[NSArray arrayWithObjects:@"詩集1",@"詩集2",@"詩集3", nil];

self.haiMingWei=[NSArray arrayWithObjects:@"這里的黎明靜悄悄",@"老人與海", nil];

self.shaShiBiYa=[NSArray arrayWithObjects:@"十四行詩集",@"慕克白",@"哈姆萊特",@"羅密歐與朱麗葉", nil];

//? ? self.dict=[NSDictionary dictionaryWithObjectsAndKeys:self.books,@"葉慈",

// ? ? ? ? ? ? ? self.haiMingWei,@"海明威",

// ? ? ? ? ? ? ? self.shaShiBiYa,@"羅密歐與朱麗葉",nil];

self.author=[NSArray arrayWithObjects:@"海明威",@"莎士比亞",@"葉慈", nil];

self.pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 20, 400, 500)];

self.pickerView.dataSource=self;

self.pickerView.delegate=self;

self.i=0;

[self.view addSubview:self.pickerView];

// Do any additional setup after loading the view, typically from a nib.

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 2;

}

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

if (component==0) {

return? self.author.count;

}

if ([self.pickerView selectedRowInComponent:0]==0) {

return? self.haiMingWei.count;

}else? if ([self.pickerView selectedRowInComponent:0]==1) {

return? self.shaShiBiYa.count;

}else

return self.books.count;

}

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

//根據(jù)row參數(shù)返回books中的元素,row參數(shù)代表列表項的編號

//因此該方法表示第幾個列表項,就使用books中的第幾個元素

if (component==0) {

return? self.author[row];

}

switch ([self.pickerView selectedRowInComponent:0]) {

case 0:

return self.haiMingWei[row];

break;

case 1:

return self.shaShiBiYa[row];

break;

case? 2:

return? self.books[row];

break;

default:

return self.haiMingWei[row];

break;

}

}

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

//使用一個UIAlertView來系那是用戶選中的列表項

if (component==0) {

[self.pickerView selectRow:row inComponent:component animated:YES];

[self.pickerView reloadComponent:1];

}

}

實現(xiàn)了單列巡回滾動的UIPickerView

.h——————————————————————

@property (nonatomic) UIPickerView* pickerView;

@property (nonatomic) UIImage* lose;

@property (nonatomic) UIImage* win;

@property (nonatomic) NSMutableArray* images;

@property (nonatomic) UIImageView* view;

@property (nonatomic) UIButton* start;

@property (nonatomic) UIImageView* image;

@property (nonatomic,strong) NSArray* imagesTemp;//原圖片序列

@property (nonatomic,strong) NSMutableArray* comArr;//控制每一列的圖片,每一序列是picker一列的所有圖片集合

.m——————————————————————

- (void)viewDidLoad {

[super viewDidLoad];

UIImage* dog=[UIImage imageNamed:@"./images/dog.png"];

UIImage* duck=[UIImage imageNamed:@"./images/duck.png"];

UIImage* elephant=[UIImage imageNamed:@"./images/elephant.png"];

UIImage* frog=[UIImage imageNamed:@"./images/frog.png"];

UIImage* mouse=[UIImage imageNamed:@"./images/mouse.png"];

UIImage* rabbit=[UIImage imageNamed:@"./images/rabbit.png"];

self.lose=[UIImage imageNamed:@"./images/lose.jpg"];

self.win=[UIImage imageNamed:@"./images/win.gif"];

self.images=[NSMutableArray arrayWithObjects:dog,duck,

elephant,frog,mouse,rabbit, nil];

self.imagesTemp=[NSMutableArray arrayWithArray:self.images];

self.comArr=[NSMutableArray arrayWithObjects:

self.imagesTemp,

self.imagesTemp,

self.imagesTemp,

self.imagesTemp,

self.imagesTemp,

self.imagesTemp

,nil];

//加載輸贏結(jié)果的圖標顯示

self.image=[[UIImageView alloc]initWithFrame:CGRectMake(190, 330, 40, 40)];

[self.view addSubview:self.image];

//加載主體

self.pickerView=[[UIPickerView alloc]initWithFrame:

CGRectMake(20, 20, 380, 200)];

self.pickerView.dataSource=self;

self.pickerView.delegate=self;

self.pickerView.userInteractionEnabled=NO;

[self.view addSubview:self.pickerView];

//加載開始按鈕

self.start=[UIButton buttonWithType:UIButtonTypeRoundedRect];

self.start.frame=CGRectMake(200, 400, 40, 50);

[self.start setTitle:@"開始" forState:UIControlStateNormal];

[self.start addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.start];

// Do any additional setup after loading the view, typically from a nib.

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 5;

}

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

return self.images.count;

}

#define kImageTag 1

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

if (view.tag!=kImageTag) {

view=[[UIImageView alloc]initWithImage:[self.comArr[component] objectAtIndex:row]];

view.tag=kImageTag;

view.userInteractionEnabled=NO;

}

return view;

}

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

return 40;

}

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

return 40;

}

-(void)clicked:(id)sender{

self.start.enabled=NO;

self.image.image=nil;

NSMutableDictionary* result=[[NSMutableDictionary alloc]

initWithCapacity:6];

NSURL* winSoundUrl=[[NSBundle mainBundle]

URLForResource:@"crunch" withExtension:@"wav"];

for (int i=0; i<5;i++) {

NSUInteger selectedVal=arc4random()%self.images.count;

//為了選擇器列之間不會互相影響,新建一個數(shù)組

NSMutableArray* arr=[NSMutableArray arrayWithArray:self.imagesTemp];

for (int j=0; j

[arr setObject: self.imagesTemp[(selectedVal+j)%self.images.count]

atIndexedSubscript:(j+3)%self.imagesTemp.count];

}

//將重新歸序的數(shù)組放入到第i列的中,重新加載

[self.comArr setObject:arr atIndexedSubscript:i];

[self.pickerView reloadComponent:i];

[self.pickerView selectRow:3 inComponent:i animated:YES];

//在result中已經(jīng)為該隨機數(shù)記錄了出現(xiàn)次數(shù)

if ([result objectForKey:[NSNumber numberWithInt:selectedVal]]) {

//獲取result中該隨機數(shù)的出現(xiàn)次數(shù)

NSUInteger newCount=[[result objectForKey:

[NSNumber numberWithInt:selectedVal] ] integerValue];

//將result中該隨機數(shù)的出現(xiàn)次數(shù)+1

[result setObject:[NSNumber numberWithInt:(newCount+1)] forKey:[NSNumber numberWithInt:selectedVal]];

}else

{

//使用result記錄該隨機數(shù)的出現(xiàn)次數(shù)為1

[result setObject:[NSNumber numberWithInt:1] forKey:[NSNumber numberWithInt:selectedVal]];

}

//使用該變量記錄隨機數(shù)的最大出現(xiàn)次數(shù)

NSUInteger maxOccurs=1;

for (NSNumber* num in [result allKeys] ) {

//只要任何隨機數(shù)的出現(xiàn)次數(shù)大于maxOccurs

if ([[result objectForKey:num]integerValue]>maxOccurs) {

//使用maxOccurs保存該隨機數(shù)的出現(xiàn)次數(shù)

maxOccurs=[[result objectForKey:num] integerValue];

}

}

//如果某個隨機數(shù)的出現(xiàn)次數(shù)大于或等于3(既是界面出現(xiàn)了三個相同圖案

if (maxOccurs>=3) {

//如果贏了延遲0.5秒執(zhí)行showWin方法,顯示結(jié)果

[self performSelector:@selector(showWin) withObject:nil afterDelay:0.5];

}else{

//

[self performSelector:@selector(showLose) withObject:nil afterDelay:0.5];

}

}

}

-(void)showWin{

self.image.image=self.win;

self.start.enabled=YES;

}

-(void)showLose{

self.image.image=self.lose;

self.start.enabled=YES;

}

//當運行后,圖片不顯示,則可能初始化加載的圖片為空,可能圖片路徑有問題,或者沒有加入到項目中

加入項目

1.使用addfileTo ? xCode左側(cè)右鍵

2.在target-BuildPhases-CompileSources 中加入圖片文件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容