UIPopoverController 用法
来源:互联网 发布:珑玲加速器mac远程协助 编辑:程序博客网 时间:2024/06/07 02:40
iOS-raywenderlich翻译-UIPopoverController 使用教程(转 中文译文)
这是三篇文章中的第二篇:帮助iPhone开发者快速的掌握几个新的类(至少对于我来说):: UISplitView,UIPopoverController和定制输入视图.
在本系列的第一篇中(firstpart of the series),我创建了一个程序,使用splitview在左边显示了一列怪物,选中怪物后,会在右边显示详细的内容。
在本文中,我将显示一个popoverview:添加一个popover,让用户可以在列表中选择颜色,以修改怪物名字的颜色。(第三篇传送门:Part3)
这里我从上次写的工程开始,如果你还没有这个工程的话,这里下载:
创建我们的颜色选取器
我们先来创建一个视图,用户可以在颜色列表中选择颜色,我将用颜色的名称来填充列表。
通过 “File\New File…”, 选择 “UIViewController subclass”, 勾选上 “Targetedfor iPad” 和 “UITableViewController subclass” ,不要勾选 “With XIB foruser interface” , 然后单击 Next. 类命名为 ColorPickerController, 然后单击Finish.
用下面的代码替换 ColorPickerController.h 中的内容:
@protocol ColorPickerDelegate- (void)colorSelected:(NSString *)color;@end@interface ColorPickerController : UITableViewController { NSMutableArray *_colors; id _delegate;} @property (nonatomic, retain) NSMutableArray *colors;@property (nonatomic, assign) id delegate; @end
在上面的代码中,我声明了一个delegate,这样当用户选择了一个颜色时,就可以通知别的类。接着是声明了两个变量/属性:一个是显示用的颜色列表,另外一个用来存储delegate。
然后对ColorPickerController.m做如下修改:
// Under @implementation@synthesize colors = _colors;@synthesize delegate = _delegate;// Add viewDidLoad like the following:- (void)viewDidLoad { [super viewDidLoad]; self.clearsSelectionOnViewWil lAppear = NO; self.contentSizeForViewInPopo ver = CGSizeMake(150.0, 140.0); self.colors = [NSMutableArray array]; [_colors addObject:@"Red"]; [_colors addObject:@"Green"]; [_colors addObject:@"Blue"];} // in numberOfSectionsInTableV iew: return 1;// in numberOfRowsInSection:return [_colors count]; // In cellForRowAtIndexPath, under configure the cell:NSString *color = [_colors objectAtIndex:indexPath.row];cell.textLabel.text = color; // In didSelectRowAtIndexPath:if (_delegate != nil) { NSString *color = [_colors objectAtIndex:indexPath.row]; [_delegate colorSelected:color];} // In deallocself.colors = nil;self.delegate = nil;
上面的大多数代码跟使用table view一样,只是多了下面这行代码::
self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);
这行代码是用来设置popover显示时的尺寸。如果不添加这行代码,默认情况下,popover会占据整个屏幕的高度(通常很大)。
显示选取器
不管你信不信,这是最难的一部分。要显示选取器,我们所需要做的是在工具栏上添加一个按钮,并添加一些代码来进行选取器的显示,以及对选择操作进行处理。
首先,我们添加一个按钮,打开 RightViewController.xib文件,然后在工具栏上添加一个barbutton。将按钮的title设置为 “Set Color”.
现在,在RightViewController.h中为这个按钮声明一个触发方法,并声明其它一些变量:
// Up top, under #import#import "ColorPickerController.h"// Modfiy class declaration@interface RightViewController : UIViewController { // Inside classColorPickerController *_colorPicker;UIPopoverController *_colorPickerPopover; // In property section@property (nonatomic, retain) ColorPickerController *colorPicker;@property (nonatomic, retain) UIPopoverController *colorPickerPopover; - (IBAction)setColorButtonTapped:(id)sender;
对了,忘记了一件事情,我们需要将按钮与这个action方法连接起来:在IB中从按钮上control-dragging到File’sOwner,然后连接到“setColorButtonTapped” 插槽.
下面对 RightViewController.m做一些改变:
// In synthesize section@synthesize colorPicker = _colorPicker;@synthesize colorPickerPopover = _colorPickerPopover;// In deallocself.colorPicker = nil;self.colorPickerPopover = nil; // Add to end of file- (void)colorSelected:(NSString *)color { if ([color compare:@"Red"] == NSOrderedSame) { _nameLabel.textColor = [UIColor redColor]; } else if ([color compare:@"Green"] == NSOrderedSame) { _nameLabel.textColor = [UIColor greenColor]; } else if ([color compare:@"Blue"] == NSOrderedSame){ _nameLabel.textColor = [UIColor blueColor]; } [self.colorPickerPopover dismissPopoverAnimated:YES];} - (IBAction)setColorButtonTapped:(id)sender { if (_colorPicker == nil) { self.colorPicker = [[[ColorPickerController alloc] initWithStyle:UITableViewStylePlain] autorelease]; _colorPicker.delegate = self; self.colorPickerPopover = [[[UIPopoverController alloc] initWithContentViewContr oller:_colorPicker] autorelease]; } [self.colorPickerPopover presentPopoverFromBarBut tonItem:sender permittedArrowDirections :UIPopoverArrowDirectionA ny animated:YES];}
OK,我对上面的代码稍加解释。所有的popover都是将以及存在的viewcontroller进行一个包装,并显示在某个确定的位置(可能会在与popover相关的某个地方显示一个箭头)。可以在setColorButtonTapped中看到具体的操作– 创建了一个颜色选取器,然后用一个popover controller对其进行包装。
接着就是调用popovercontroller的presentPopoverFromBarBut
当用户完成选择操作后,通过点击popover以外的任何地方,就可以自动的将这个popover隐藏起来。如果用户选择了某个颜色,我们希望能把popover隐藏起来,那么我们只需要在适当的地方调用dismissPopoverAnimated方法即可(最好在设置颜色的地方)。
上面就是所有内容了!编译并运行程序,然后点击“Set Color”按钮,将会看到如下的一个popover画面:
你会发现在用户需要编辑一个字段或者对某个设置进行开关时,在iPad中只需要一小点空间,而在iPhone中,则是通过UINavigationController导航到下一级画面进行修改或设置。在iPad文档中这称为“扁平化的层次结构”。
- UIPopoverController 用法
- ios UIPopoverController 用法、内存解析
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController
- UIPopoverController在ARC环境下用法注意
- 自定义 UIPopoverController
- UIPopoverController优化
- UIPopoverController.h
- 自定义 UIPopoverController
- Java Inner Class
- 在黑暗之间
- 因为这里是郊区
- 10 Books that could Make you a Better C++ Programmer
- 5天学会jaxws-webservice编程第四天
- UIPopoverController 用法
- 基本的安全和HTTP认证
- Hdu 2577 How to Type (DP)(对题意有些疑问。。)
- iOS开发之基础类的简单初始化方法
- oracle 11gR2中Flash cache特性 【转】
- C++中的new和delete——读书笔记总结
- mysql 一条sql语句插入多条记录
- javascript解析json
- [leetcode]Combination组合