iOS-raywenderlich翻譯-UIPopoverController 使用教程

來源:互聯網
上載者:User

建立我們的色彩選擇器我們先來建立一個視圖,使用者可以在顏色列表中選擇顏色,我將用顏色的名稱來填充列表。通過 “File\New File…”, 選擇 “UIViewController subclass”, 勾選上 “Targeted for iPad” 和 “UITableViewController subclass” ,不要勾選 “With XIB for user interface” , 然後單擊 Next. 類命名為 ColorPickerController, 然後單擊 Finish.用下面的代碼替換 ColorPickerController.h 中的內容:@protocol ColorPickerDelegate- (void)colorSelected:(NSString *)color;@end  @interface ColorPickerController : UITableViewController {    NSMutableArray *_colors;    id<ColorPickerDelegate> _delegate;} @property (nonatomic, retain) NSMutableArray *colors;@property (nonatomic, assign) id<ColorPickerDelegate> 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檔案,然後在工具列上添加一個bar button。將按鈕的title設定為 “Set Color”.現在,在RightViewController.h中為這個按鈕聲明一個觸發方法,並聲明其它一些變數:// Up top, under #import#import "ColorPickerController.h" // Modfiy class declaration@interface RightViewController : UIViewController <MonsterSelectionDelegate,      UISplitViewControllerDelegate, ColorPickerDelegate> { // 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’s Owner,然後串連到“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都是將以及存在的view controller進行一個封裝,並顯示在某個確定的位置(可能會在與popover相關的某個地方顯示一個箭頭)。可以在setColorButtonTapped中看到具體的操作 – 建立了一個色彩選擇器,然後用一個popover controller對其進行封裝。接著就是調用popover controller的presentPopoverFromBarButtonItem方法將其顯示在view中。當使用者完成選擇操作後,通過點擊popover以外的任何地方,就可以自動的將這個popover隱藏起來。如果使用者選擇了某個顏色,我們希望能把popover隱藏起來,那麼我們只需要在適當的地方調用 dismissPopoverAnimated方法即可(最好在設定顏色的地方)。上面就是所有內容了!編譯並運行程式,然後點擊“Set Color”按鈕,將會看到如下的一個popover畫面:你會發現在使用者需要編輯一個欄位或者對某個設定進行開關時,在iPad中只需要一小點空間,而在iPhone中,則是通過UINavigationController導航到下一級畫面進行修改或設定。在iPad文檔中這稱為“扁平化的階層”。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.