之前自己也使用过,但是感觉很是别扭,还要调试什么尺寸的这个问题,今天看了cookbook的方法,明白了,该如何使用,来个例子好了。
UIPickerViewDemoViewController.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #import <UIKit/UIKit.h> #define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f] #define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] @interface UIPickerViewDemoViewController : UIViewController <UIPickerViewDataSource ,UIPickerViewDelegate ,UIActionSheetDelegate >@end
UIPickerViewDemoViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #import "UIPickerViewDemoViewController.h" @interface UIPickerViewDemoViewController ()@end @implementation UIPickerViewDemoViewController - (void )viewDidLoad { [super viewDidLoad]; self .navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; self .navigationItem.rightBarButtonItem = BARBUTTON(@"操作" , @selector (action:)); } - (void )didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(BOOL ) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation )toInterfaceOrientation{ return YES ; } #pragma mark - UIPickerView Method -(NSInteger ) numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 3 ; } -(NSInteger ) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger )component{ return 20 ; } -(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger )row forComponent:(NSInteger )component{ return [NSString stringWithFormat:@"%@-%d" , (component == 1 ? @"R" : @"L" ), row]; } -(void ) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger )buttonIndex{ UIPickerView *pickerView = (UIPickerView *)[actionSheet viewWithTag:101 ]; self .title = [NSString stringWithFormat:@"L%d-R%d-L%d" ,[pickerView selectedRowInComponent:0 ], [pickerView selectedRowInComponent:1 ], [pickerView selectedRowInComponent:2 ]]; } -(void ) action:(id )sender{ NSString *title = UIDeviceOrientationIsLandscape ([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ; UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"选择此项" , nil ]; [actionsheet showInView:self .view]; UIPickerView *pickerView = [[UIPickerView alloc] init]; pickerView.tag = 101 ; pickerView.delegate = self ; pickerView.dataSource = self ; pickerView.showsSelectionIndicator = YES ; [actionsheet addSubview:pickerView]; CFShow ((__bridge CFTypeRef )(NSStringFromCGRect (pickerView.frame))); } @end
在自己的项目中,没有任何强制要求的前提下,完全是可以做出很华丽的应用的,要掌握正确的使用方式。
这里面其实重点是要使用代理方法UIPickerViewDataSource,UIPickerViewDelegate,UIActionSheetDelegate这个三个在这里是不能少的。