iPhone应用开发之UIPickerView使用DEMO是本文要介绍的内容,内容不多,本文基于代码实现的内容,主要编写选择器的实现。从生肖选择、学历选择等来实现UIPickerView的用法,来看详细内容。
- @interface Set_starViewController : UIViewController<UIPickerViewDelegate>{
- UIPickerView *pickerview;
- UILabel *contentview;
- NSArray *content;//星座
- id _delegate;
- int _type;
- }
- -(id)initWithDelegate:(id)delegate type:(int)type;
- - (void)setString:(id)sender;
- @end
- // Set_starViewController.m
- //
- // Created by AmorYin on 10-11-23.
- // Copyright 2010 __MyCompanyName__. All rights reserved.
- //
- #import "Set_starViewController.h"
- @implementation Set_starViewController
- -(id)initWithDelegate:(id)delegate type:(int)type
- {
- self = [super init];
- if (self != nil){
- UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 20)];
- titleText.backgroundColor = [UIColor clearColor];
- titleText.textAlignment = UITextAlignmentCenter;
- titleText.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.0];
- [titleText setFont:[UIFont systemFontOfSize:15.0]];
- [titleText setText:@"星座"];
- self.navigationItem.titleView=titleText;
- [titleText release];
- _delegate = delegate;
- UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 62.0, 32.0)];
- [back setBackgroundImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateNormal];
- [back setBackgroundImage:[UIImage imageNamed:@"返回按钮按下.png"] forState:UIControlStateHighlighted];
- [back setBackgroundImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateDisabled];
- [back addTarget:_delegate
- action:@selector(backView)
- forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithCustomView:back];
- [[self navigationItem] setLeftBarButtonItem:add];
- [add release];
- [back release];
- UIBarButtonItem *save = nil;
- save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
- target:self
- action:@selector(setString:)];
- self.navigationItem.rightBarButtonItem = save;
- save = nil;
- [save release];
- if (type == 0) {
- content = [[NSArray alloc] initWithObjects:@"水瓶座",@"双鱼座",@"白羊座",@"金牛座"
- ,@"双子座",@"巨蟹座",@"狮子座",@"处女座"
- ,@"天秤座",@"天蝎座",@"射手座",@"山羊座",nil];
- _type = 0;
- }else {
- content = [[NSArray alloc] initWithObjects:@"初中",@"高中",@"中技",@"中专",@"大专",@"本科",@"MBA",@"硕士"
- ,@"其他",nil];
- _type = 1;
- }
- }
- return self;
- }
- -(void) loadView
- {
- UIView *myview = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
- myview.autoresizesSubviews = YES;
- [myview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
- self.view = myview;
- // 设置选择器
- pickerview = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 150.0, 320.0, 216.0)];
- pickerview.delegate = self;
- pickerview.showsSelectionIndicator = YES;
- [self.view addSubview:pickerview];
- contentview = [[UILabel alloc] initWithFrame:CGRectMake(80.0, 80.0, 100.0, 40.0)];
- contentview.backgroundColor = [UIColor clearColor];
- [self.view addSubview: contentview];
- }
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)dealloc {
- [super dealloc];
- [contentview release];
- [pickerview release];
- [content release];
- }
- #pragma mark -
- #pragma mark 处理方法
- // 返回显示的列数
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
- {
- return 1;
- }
- // 返回当前列显示的行数
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- return [content count];
- }
- // 设置当前行的内容,若果行没有显示则自动释放
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {
- return [content objectAtIndex:row];
- }
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
- {
- //NSString *result = [pickerView pickerView:pickerView titleForRow:row forComponent:component];
- NSString *result = nil;
- result = [content objectAtIndex:row];
- NSLog(@"result: %@",result);
- contentview.text = result;
- [result release];
- }
- - (void)setString:(id)sender
- {
- // 传递值
- if (_type ==0) {
- [_delegate setSomthing:contentview.text name:@"star"];
- }else {
- [_delegate setSomthing:contentview.text name:@"xueli"];
- }
- [self.navigationController popViewControllerAnimated:YES];
- [self.navigationController pushViewController:_delegate animated:YES];
- [self.navigationController setNavigationBarHidden:NO];
- }
- @end
小结:iPhone应用开发之UIPickerView使用DEMO的内容介绍完了,希望通过本文的学习对你有所帮助!