iOS開發UI篇—popoverController簡單介紹

來源:互聯網
上載者:User

標籤:

一、簡單介紹

1.什麼是UIPopoverController

是iPad開發中常見的一種控制器(在iPhone上不允許使用)

跟其他控制器不一樣的是,它直接繼承自NSObject,並非繼承自UIViewController

它只佔用部分螢幕空間來呈現資訊,而且顯示在螢幕的最前面

2.使用步驟

要想顯示一個UIPopoverController,需要經過下列步驟

(1)設定內容控制器

  由於UIPopoverController直接繼承自NSObject,不具備可視化的能力。因此UIPopoverController上面的內容必須由另外一個繼承自UIViewController的控制器來提供,這個控制器稱為“內容控制器”

 (2)設定內容的尺寸

  顯示出來佔據多少螢幕空間

(3)顯示,即從哪個地方冒出來

 

二、具體的步驟

程式碼範例:

建立一個ipad項目,編寫如下代碼: 

建立一個繼承自UITableView的控制器,讓其作為popoverController的內容控制器。

YYMenuViewController.m檔案

 1 // 2 //  YYMenuViewController.m 3 //  01-PopoverController簡單介紹 4 // 5 //  Created by apple on 14-8-17. 6 //  Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8  9 #import "YYMenuViewController.h"10 11 @interface YYMenuViewController ()12 @property(nonatomic,strong)NSArray *menus;13 @end14 15 @implementation YYMenuViewController16 17 -(NSArray *)menus18 {19     if (_menus==nil) {20         [email protected][@"列表1",@"列表2",@"列表3",@"列表4"];21     }22  return _menus;23 }24 - (void)viewDidLoad25 {26     [super viewDidLoad];27 }28 29 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView30 {31     return 1;32 }33 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section34 {35     return self.menus.count;36 }37 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath38 {39     static NSString *ID=@"ID";40     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];41     if (cell==nil) {42         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];43     }44     45     cell.textLabel.text=self.menus[indexPath.row];46     return cell;47 }48 49 @end

YYViewController.m檔案

 1 // 2 //  YYViewController.m 3 //  01-PopoverController簡單介紹 4 // 5 //  Created by apple on 14-8-17. 6 //  Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "YYMenuViewController.h"11 12 @interface YYViewController ()13 @property(nonatomic,strong)UIPopoverController *popover;14 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21 }22 23 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event24 {25     //1.建立一個內容控制器26     YYMenuViewController *menuVc=[[YYMenuViewController alloc]init];27     28     //2.建立一個popoverController,並設定其內容控制器29     self.popover=[[UIPopoverController alloc]initWithContentViewController:menuVc];30     31     //3.設定尺寸32     self.popover.popoverContentSize=CGSizeMake(300, 200);33     34     //4.顯示35     [self.popover presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];36 }37 @end

 

實現效果如:
  

說明:在storyboard中添加了導航控制器,並添加了兩個按鈕。

  

 

三、常見報錯

在popover的使用過程中,經常會遇到這個錯誤

  -[UIPopoverController dealloc] reached while popover is still visible.

錯誤的大體意思是:popover在仍舊可見的時候被銷毀了(調用了dealloc)

從錯誤可以得出的結論

  當popover仍舊可見的時候,不準銷毀popover對象

  在銷毀popover對象之前,一定先讓popover消失(不可見)  

如:在上述代碼中,如果不適用全域變數popover,那麼將會出現上面的錯誤。

iOS開發UI篇—popoverController簡單介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.