iPhone开发【十八】自定义视图之ActionSheet中使用PickerView
发布日期:2021-09-28 18:46:32 浏览次数:10 分类:技术文章

本文共 4740 字,大约阅读时间需要 15 分钟。

转载请注明出处,原文网址: 作者:张燕广

实现的功能:1)打开ActionSheet后展示PickerView,进行选择操作。

关键词:ActionSheet PickerView

1、新建一个Sigle View Application,命名为PickerInActionSheet,工程结构如下:

2、修改ViewController.xib,添加一个TextField控件。

3、修改ViewController.h,如下:

[java] 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>  
  4.   
  5. @property(retain,nonatomic)UIPickerView *picker;  
  6. @property(retain,nonatomic)UIActionSheet *actionSheet;  
  7. @property(retain,nonatomic)IBOutlet UITextField *textField;  
  8.   
  9. @property(nonatomic,retain)NSDictionary *appleDevices;    
  10. @property(nonatomic,retain)NSArray *deviceCategory;    
  11. @property(nonatomic,retain)NSArray *deviceName;    
  12.   
  13. -(IBAction)showActionSheet:(id)sender;  
  14. @end  
连接输出口textField及操作showActionSheet,如下:

4、修改ViewController.m,如下:

[cpp] 
  1. #import "ViewController.h"  
  2.   
  3. #define kDeviceCategory 0    
  4. #define kDeviceName 1   
  5.   
  6. @interface ViewController ()  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController  
  11. @synthesize appleDevices;    
  12. @synthesize deviceCategory;    
  13. @synthesize deviceName;   
  14. @synthesize picker;  
  15. @synthesize actionSheet;  
  16. @synthesize textField;  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view, typically from a nib.  
  22.     [self initData];  
  23. }  
  24.   
  25. //初始化数据  
  26. -(void)initData{  
  27.     textField.placeholder = @"请点击。。。";  
  28.     textField.textAlignment = UITextAlignmentCenter;  
  29.       
  30.     NSArray *array1 = [NSArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",nil];    
  31.     NSArray *array2 = [NSArray arrayWithObjects:@"Mac",@"iMac",@"Mac Mini",@"Mac Pro",nil];    
  32.     NSDictionary  *dictionary= [NSDictionary dictionaryWithObjectsAndKeys:array1,@"Mobile",array2,@"Computers",nil];    
  33.     appleDevices = [[NSDictionary alloc]initWithDictionary:dictionary copyItems:YES];    
  34.       
  35.     NSArray *components = [self.appleDevices allKeys];    
  36.     NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];    
  37.     self.deviceCategory = sorted;    
  38.       
  39.     NSString *selectedCategory = [self.deviceCategory objectAtIndex:0];    
  40.     self.deviceName = [self.appleDevices objectForKey:selectedCategory];    
  41. }  
  42.   
  43. //初始化视图  
  44. -(void)showActionSheetPicker{  
  45.     //在title中加入多个换行,给picker留出空间,否则picker会盖住ActionSheet的button  
  46.     NSString *title = @"请选择设备\n\n\n\n\n\n\n\n\n\n\n\n\n\n";  
  47.     actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil,nil];  
  48.       
  49.     picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 220)];  
  50.     picker.delegate = self;  
  51.     picker.dataSource = self;  
  52.     picker.showsSelectionIndicator = YES;  
  53.       
  54.     [actionSheet addSubview:picker];  
  55.     [actionSheet showInView:self.view];  
  56. }  
  57.   
  58. -(void)dealloc{  
  59.     self.deviceName = nil;  
  60.     self.deviceCategory = nil;  
  61.     self.appleDevices = nil;  
  62.     self.picker = nil;  
  63. }  
  64.   
  65. - (void)viewDidUnload  
  66. {  
  67.     [super viewDidUnload];  
  68.     // Release any retained subviews of the main view.  
  69. }  
  70.   
  71. //显示actionSheet  
  72. -(IBAction)showActionSheet:(id)sender{  
  73.     [self showActionSheetPicker];  
  74. }  
  75.   
  76. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  77. {  
  78.     //强制用竖屏模式  
  79.     return UIInterfaceOrientationIsPortrait(interfaceOrientation);  
  80. }  
  81.   
  82.   
  83. #pragma mark Picker Data Source Methods  
  84. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{  
  85.     return 2;  
  86. }  
  87. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{  
  88.     if(component == kDeviceCategory){    
  89.         return [self.deviceCategory count];    
  90.     }else{    
  91.         return [self.deviceName count];    
  92.     }    
  93. }  
  94. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{  
  95.     if(component == kDeviceCategory){    
  96.         return [self.deviceCategory objectAtIndex:row];    
  97.     }else{    
  98.         return [self.deviceName objectAtIndex:row];    
  99.     }    
  100. }  
  101.   
  102. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{  
  103.     if(component==kDeviceCategory){  
  104.         return 150;  
  105.     }else{  
  106.         return 170;  
  107.     }  
  108. }  
  109. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{  
  110.     return 35;  
  111. }  
  112.   
  113. //显示picker中的数据  
  114. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{  
  115.     NSString *inputString;  
  116.     if(component == kDeviceCategory){    
  117.         NSString *selectedCategory = [self.deviceCategory objectAtIndex:row];    
  118.         NSArray *array = [self.appleDevices objectForKey:selectedCategory];    
  119.         self.deviceName = array;    
  120.         [self.picker selectRow:0 inComponent:kDeviceName animated:YES];    
  121.         [self.picker reloadComponent:kDeviceName];    
  122.           
  123.         inputString = selectedCategory;  
  124.     }else if(component == kDeviceName){  
  125.         NSUInteger selectedCategoryRow = [pickerView selectedRowInComponent:kDeviceCategory];  
  126.         NSString *selectedCategory = [self.deviceCategory objectAtIndex:selectedCategoryRow];  
  127.           
  128.         inputString = [NSString stringWithFormat:@"%@-%@",selectedCategory,[self.deviceName objectAtIndex:row]];  
  129.     }  
  130.     //给文本框设置值  
  131.     self.textField.text=inputString;  
  132. }    
  133.       
  134.   
  135. - (void)didReceiveMemoryWarning {  
  136.     [super didReceiveMemoryWarning];      
  137. }  
  138. @end  
5
、运行效果如下:

需要源码的网友请留言哦

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8568201 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
下一篇:散步 —— 莫怀戚

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月22日 03时28分14秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章