IOS UIPickerView 用法
IOS UIPickerView:
1 uipickerview
2 datasource
3 delegate
4 yourself make picker view;
1 .h file
@interface ViewController : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
{
UILabel *label;
UIPickerView *pickerview;
NSArray *fontAry;
}
@property (nonatomic,retain) IBOutlet UILabel *label;
@property (nonatomic,retain) IBOutlet UIPickerView *pickerview;
@end
2 .m file
@synthesize label;
@synthesize pickerview;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
fontAry=[[UIFont familyNames] retain];
pickerview.delegate=self;
pickerview.dataSource=self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.label=nil;
self.pickerview=nil;
}
-(void)dealloc
{
[label retain];
[pickerview retain];
[super dealloc];
}
#pragma mark pickerview function
/* return cor of pickerview*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
/*return row number*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [fontAry count];
}
/*return component row str*/
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [fontAry objectAtIndex:row];
}
/*choose com is component,row's function*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(@"font %@ is selected.",row);
NSString *fontname=[fontAry objectAtIndex:row];
label.font=[UIFont fontWithName:fontname size:20.0f];
label.text=[NSString stringWithFormat:@"selected font is : %@ ",fontname];
}
3 run result