UIPopoverController 用法

来源:互联网 发布:珑玲加速器mac远程协助 编辑:程序博客网 时间:2024/06/07 02:40

iOS-raywenderlich翻译-UIPopoverController 使用教程(转 中文译文)

注:本文译自http://www.raywenderlich.com/1056/ipad-for-iphone-developers-101-uipopovercontroller-tutorial
13 MAY 2010

We'll Create a Popover Like This.

这是三篇文章中的第二篇:帮助iPhone开发者快速的掌握几个新的类(至少对于我来说):: UISplitView,UIPopoverController和定制输入视图.

在本系列的第一篇中(firstpart of the series),我创建了一个程序,使用splitview在左边显示了一列怪物,选中怪物后,会在右边显示详细的内容。

在本文中,我将显示一个popoverview:添加一个popover,让用户可以在列表中选择颜色,以修改怪物名字的颜色。(第三篇传送门:Part3)

这里我从上次写的工程开始,如果你还没有这个工程的话,这里下载: graba copy .

创建我们的颜色选取器

我们先来创建一个视图,用户可以在颜色列表中选择颜色,我将用颜色的名称来填充列表。

通过 “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.clearsSelectionOnViewWillAppear = NO;    self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);    self.colors = [NSMutableArray array];    [_colors addObject:@"Red"];    [_colors addObject:@"Green"];    [_colors addObject:@"Blue"];} // in numberOfSectionsInTableView: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]             initWithContentViewController:_colorPicker] autorelease];                   }    [self.colorPickerPopover presentPopoverFromBarButtonItem:sender         permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}

OK,我对上面的代码稍加解释。所有的popover都是将以及存在的viewcontroller进行一个包装,并显示在某个确定的位置(可能会在与popover相关的某个地方显示一个箭头)。可以在setColorButtonTapped中看到具体的操作– 创建了一个颜色选取器,然后用一个popover controller对其进行包装。

接着就是调用popovercontroller的presentPopoverFromBarButtonItem方法将其显示在view中。

当用户完成选择操作后,通过点击popover以外的任何地方,就可以自动的将这个popover隐藏起来。如果用户选择了某个颜色,我们希望能把popover隐藏起来,那么我们只需要在适当的地方调用dismissPopoverAnimated方法即可(最好在设置颜色的地方)。

上面就是所有内容了!编译并运行程序,然后点击“Set Color”按钮,将会看到如下的一个popover画面:

Screenshot of Popover View

你会发现在用户需要编辑一个字段或者对某个设置进行开关时,在iPad中只需要一小点空间,而在iPhone中,则是通过UINavigationController导航到下一级画面进行修改或设置。在iPad文档中这称为“扁平化的层次结构”。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 球球大作战找不到团战服务器怎么办 小孩的腰有点弯怎么办 小朋友不听话被老师罚站怎么办 幼儿园小朋友被老师罚站怎么办 生完小孩弯腰驼背怎么办 小孩爱捡垃圾是怎么办 腰扭了不敢弯腰怎么办 小人狗在背后骂我怎么办 微信表情缺失了怎么办 动图过大 微信 怎么办 我能怎么办图片带字 我该怎么办图片带字 学化妆找不到模特练妆怎么办? cf进房间闪退怎么办 手游cf账号封了怎么办 大门牙缺了一块怎么办 缺了一颗牙齿怎么办 CF购买医疗包竞猜币没到账怎么办 CF手游昵称不合法怎么办 微信gif尺寸过大怎么办 微信表情上限300怎么办 太受欢迎了怎么办快穿 兔宝宝沾上人气味怎么办 我该怎么办的文字图片 爱奇艺缓存视频显示下载失败怎么办 把老公惹生气了怎么办 苹果x用电量太快怎么办 小中考地生没过怎么办 如果遇到不负责的语文老师怎么办 孩子的语文老师教的不好怎么办 刚买的小猫很凶怎么办 2月幼犬不吃东西怎么办 小狗狗不吃狗粮怎么办 母猫不会照顾小猫怎么办 刚买的小狗拉稀怎么办 母兔子吃小兔子怎么办 照片照出来背亮景人是黑的怎么办 手机透明壳变黄了怎么办 ae视频渲染太慢怎么办 3ce口红太干了怎么办 中考误用0.38mm的笔怎么办