iphone常用控件之UIPickerVIew
1.点击按钮触发allcountry事件:
-(void)allcoutry { //界面设计 picker_background=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; UIImageView *navigationItem=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SchoolSearch_AllPleace_navBar.png"]]; navigationItem.frame=CGRectMake(0, 0, 320, 43); [picker_background addSubview:navigationItem]; [navigationItem release]; UIImage *cancel_image=[UIImage imageNamed:@"SchoolSearch_AllPleace_cancelButton.png"]; UIButton *cancel=[UIButton buttonWithType:UIButtonTypeCustom]; cancel.frame=CGRectMake(10, 5.5,55.5, 32.5); [cancel setImage:cancel_image forState:UIControlStateNormal]; [cancel_image release]; [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside]; [cancel setTitle:nil forState:UIControlStateNormal]; [picker_background addSubview:cancel]; UIImage *complete_image=[UIImage imageNamed:@"SchoolSearch_AllPleace_doneButton.png"]; UIButton *complete=[UIButton buttonWithType:UIButtonTypeCustom]; complete.frame=CGRectMake(255.5,5.5,55.5,32.5); [complete setImage:complete_image forState:UIControlStateNormal]; [complete_image release]; [complete addTarget:self action:@selector(complete) forControlEvents:UIControlEventTouchUpInside]; [picker_background addSubview:complete]; //UIPickerView picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320,100036)]; picker.delegate=self; picker.showsSelectionIndicator=YES; [picker_background addSubview:picker]; [self.view addSubview:picker_background];
}
-(void)cancel { [picker_background removeFromSuperview]; } -(void)complete { //显示所选择的区县名 [picker_background removeFromSuperview]; if ([Countru_name length]==0) { Countru_name = @"全部区县"; } [allcountry setTitle:Countru_name forState:UIControlStateNormal]; }
// 返回显示的列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView { return 1; } // 返回当前列显示的行数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [picker_name count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return (NSString *)[picker_name objectAtIndex:row]; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"row %d",row); pickerRow=row; allCountry_id=(NSString *)[picker_nameid objectAtIndex:row]; Countru_name=(NSString *)[picker_name objectAtIndex:row]; //picker_name 为名字数组 }
效果如下:
2.点击UITextField触发PIckerView事件:
-(void)viewDidLoad{ UITextField* Equeipment=[[UITextField alloc] initWithFrame:CGRectMake(94, 201, 170, 17)]; Equeipment.font=[UIFont systemFontOfSize:16]; Equeipment.delegate=self; Equeipment.text=@"非常好(5 分)"; Equeipment.backgroundColor=[UIColor clearColor]; [self.view addSubview: Equeipment]; [Equeipment release]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } -(void)textFieldDidEndEditing:(UITextField *)textField { } -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if (textField==qualified_rate) { picker_background=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; UIImage *cancel_image=[UIImage imageNamed:@"SchoolSearch_AllPleace_cancelButton.png"]; UIButton *cancel=[[UIButton alloc]init]; cancel.frame=CGRectMake(10, 5.5,55.5, 32.5); [cancel setImage:cancel_image forState:UIControlStateNormal]; [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside]; [cancel setTitle:nil forState:UIControlStateNormal]; [picker_background addSubview:cancel]; [picker release]; UIImage *complete_image=[UIImage imageNamed:@"SchoolSearch_AllPleace_doneButton.png"]; UIButton *complete=[[UIButton alloc] init]; complete.frame=CGRectMake(255.5,5.5,55.5,32.5); [complete setImage:complete_image forState:UIControlStateNormal]; [complete addTarget:self action:@selector(complete) forControlEvents:UIControlEventTouchUpInside]; [picker_background addSubview:complete]; [complete release]; picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320,100036)]; picker.delegate=self; picker.showsSelectionIndicator=YES; [picker_background addSubview:picker]; [self.view addSubview:picker_background]; [picker_background release]; return NO; } return YES; } -(void)cancel { [picker_background removeFromSuperview]; } -(void)complete2 { Equeipment.text=tempText; [picker_background removeFromSuperview]; } #pragma UIPickerView Delegate - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView { return 1; } // 返回当前列显示的行数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [array count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [array objectAtIndex:row]; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { tempText=[array objectAtIndex:row]; }