本文主要是介绍009 在Xcode4.5上创建IOS6.0应用 (高级控件 拾取器),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
IOS中的高级控件拾取器
@interface FirstViewController : UIViewController{IBOutlet UIDatePicker *uidataPicker;
}@property(nonatomic,retain)UIDatePicker *uidataPicker;-(IBAction)onClick:(id)sender;@end
@synthesize uidataPicker;-(IBAction)onClick:(id)sender{NSDate *selected = [uidataPicker date];NSString *message = [[NSString alloc] initWithFormat:@"您选择的日期是: %@",selected];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择的日期"message:message delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[message release];
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"First", @"First");self.tabBarItem.image = [UIImage imageNamed:@"first"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[uidataPicker release];[super dealloc];
}@end
第四步:为其控件连线
< UIPickerViewDataSource,UIPickerViewDelegate>
源码如下:
@interface SecondViewController : UIViewController< UIPickerViewDataSource,UIPickerViewDelegate>{IBOutlet UIPickerView *pickView;NSArray * arr;
}@property (nonatomic,retain)UIPickerView *pickView;
@property (nonatomic,retain)NSArray * arr;-(IBAction)onClick:(id)sender;
@end
然后在SecondViewController.m文件中编码
实现协议后必须实现协议里面的三个方法分别为
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{}
然后必须在初始化的时候创建一个定义了拾取器中内容的数组
也就拾在ViewDidLoad方法中
NSArray *array = [[NSArrayalloc]initWithObjects:@"欧洲",@"南美洲", @"非洲",@"北美洲",@"亚洲",@"大洋洲",nil];
源代码如下:
@implementation SecondViewController@synthesize pickView;
@synthesize arr;-(IBAction)onClick:(id)sender{NSInteger row = [pickView selectedRowInComponent:0];NSString *selected = [arr objectAtIndex:row];NSString *title = [[NSString alloc] initWithFormat:@"您选择了 %@!",selected];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择的日期"message:titledelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[title release];[selected release];
}//pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{return [arr objectAtIndex:row];
}//pragma mark 实现协议UIPickerViewDataSource方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 1;
}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{return [arr count];
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"Second", @"Second");self.tabBarItem.image = [UIImage imageNamed:@"second"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];NSArray *array = [[NSArray alloc] initWithObjects:@"欧洲",@"南美洲",@"非洲",@"北美洲",@"亚洲",@"大洋洲",nil];self.arr = array;[array release];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[pickView release];[arr release];[super dealloc];
}@end
最后再次提醒一定要加入上图第三步中的连线
最后再看看效果图
第三种:双标签拾取器(如果上面那个搞清楚了这个就相对比较简单)
由于这个只是把拾取器从1个变成了2个所以在定义集合变量的时候要定义两个
ThreeViewController.h
@interface ThreeViewController : UIViewController< UIPickerViewDataSource,UIPickerViewDelegate>{IBOutlet UIPickerView *pickView;NSArray * arr1;NSArray * arr2;
}@property (nonatomic,retain)UIPickerView *pickView;
@property (nonatomic,retain)NSArray * arr1;
@property (nonatomic,retain)NSArray * arr2;-(IBAction)onClick:(id)sender;@end
ThreeViewController.m文件也只要做点点的修改就行了,说白了,就拾一个变两个的过程
@implementation ThreeViewController@synthesize pickView;
@synthesize arr1;
@synthesize arr2;-(IBAction)onClick:(id)sender{NSInteger row1 = [pickView selectedRowInComponent:0];NSInteger row2 = [pickView selectedRowInComponent:1];NSString *selected1 = [arr1 objectAtIndex:row1];NSString *selected2 = [arr2 objectAtIndex:row2];NSString *title = [[NSString alloc] initWithFormat:@"您选择了%@中的%@!",selected1,selected2];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择"message:titledelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[title release];[selected1 release];[selected2 release];
}//pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{if (component == 0) {return [arr1 objectAtIndex:row];}else if(component == 1){return [arr2 objectAtIndex:row];}else{return nil;}
}//pragma mark 实现协议UIPickerViewDataSource方法
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 2;
}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{if (component ==0) {return [arr1 count];}else if (component == 1){return [arr2 count];}else{return 0;}
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"Three", @"Three");self.tabBarItem.image = [UIImage imageNamed:@"first"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];NSArray *array1 = [[NSArray alloc] initWithObjects:@"欧洲",@"南美洲",@"非洲",@"北美洲",@"亚洲",@"大洋洲",nil];NSArray *array2 = [[NSArray alloc] initWithObjects:@"足球",@"篮球",@"羽毛球",@"乒乓球",@"保龄球",@"铅球",nil];self.arr1 = array1;self.arr2 = array2;[array1 release];[array2 release];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[pickView release];[arr1 release];[arr2 release];[super dealloc];
}@end
第四种:双标签拾取器有联动效果(如果上面那个搞清楚了这个就相对比较简单)
有联动效果的拾取器相对与第三种稍微麻烦一点
首先来看看源代码
FourViewController.h
@interface FourViewController : UIViewController< UIPickerViewDataSource,UIPickerViewDelegate>{NSDictionary *date;IBOutlet UIPickerView *pickView;NSArray * arr1;NSArray * arr2;
}@property(nonatomic,retain)NSDictionary *date;@property (nonatomic,retain)UIPickerView *pickView;
@property (nonatomic,retain)NSArray * arr1;
@property (nonatomic,retain)NSArray * arr2;-(IBAction)onClick:(id)sender;
@end
FourViewController.m
@implementation FourViewController@synthesize date;
@synthesize pickView;
@synthesize arr1;
@synthesize arr2;//pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{if (component == 0) {return [arr1 objectAtIndex:row];}else if(component == 1){return [arr2 objectAtIndex:row];}else{return nil;}
}//pragma mark 实现协议UIPickerViewDataSource方法
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 2;
}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{if (component ==0) {return [arr1 count];}else if (component == 1){return [arr2 count];}else{return 0;}
}//实现联动的效果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{if(component ==0){NSString *selectedCol1 = [self.arr1 objectAtIndex:row];NSArray *array = [self.date objectForKey:selectedCol1];self.arr2 = array;[self.pickView reloadComponent:1];}
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"Four", @"Four");self.tabBarItem.image = [UIImage imageNamed:@"second"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];NSBundle * bundle = [NSBundle mainBundle];NSString * plistPath = [bundle pathForResource:@"statedictionary"ofType:@"plist"];NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];self.date = dict;[dict release];NSArray *col1 = [self.date allKeys];//下面这个方法是排序的意思NSArray *sorted = [col1 sortedArrayUsingSelector:@selector(compare:)];self.arr1 = sorted;NSArray *seleteCol1 = [self.arr1 objectAtIndex:0];NSArray *col2 = [self.date objectForKey:seleteCol1];self.arr2 = col2;}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[date release];[pickView release];[arr1 release];[arr2 release];[super dealloc];
}
@end
代码中与上一个有小区别的是
多了一个联动是调用的方法
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
还多了一个变量这个变量是字典变量
唯一不同的是这次获取内容是从文件中获取的
下面就来介绍一下定义文件的过程
第一步:看图建立文件
第二步:编辑文件中的内容(由于Xcode为我们提供了比较好的编辑工具所以文件编辑过程就不多做介绍)
但是要注意下图红框中的内容一但填错就得不到数据
在选择Root类型的时候一定要选择字典类型
最后看两张效果图片
最后拾取器就这样介绍完了,看了以上的介绍一定觉得拾取器还是比较简单的控键把。
这篇关于009 在Xcode4.5上创建IOS6.0应用 (高级控件 拾取器)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!