【iOS開發-106】UIPopoverController,[UIPopoverController dealloc] reached while popover is still visible

來源:互聯網
上載者:User

標籤:

(1)UIPopoverController是iPad特有的彈框類似的類,它沒有視圖,因為直接繼承自NSObject,所以在初始化的時候需要設定內容控制器。如下:

-(UIPopoverController *)titlePop{    if (_titlePop==nil) {        UITableViewController *tableVc=[[UITableViewController alloc]init];        _titlePop=[[UIPopoverController alloc]initWithContentViewController:tableVc];    }    return _titlePop;}

(2)UIPopoverController有2種使用方法,一種是圍繞BarButtonItem的,一種是圍繞Rect的,當然Rect的範圍很廣。如下:

-(void)menuClick:(UIBarButtonItem *)sender{    //直接圍繞BarButtonItem的    [self.menuPop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}-(void)titleClick:(UIButton *)btn{    //注意rect和inView的關係    [self.titlePop presentPopoverFromRect:btn.frame inView:btn.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];    //[self.titlePop presentPopoverFromRect:btn.bounds inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}

(3)UIPopoverController還有一些其他屬性,比如是否可見等。

//還有以下方法。設定內容視圖控制器//[self.menuPop setContentViewController:nav animated:YES];        //設定popover大小//[self.menuPop setPopoverContentSize:CGSizeMake(100, 100)];//self.menuPop.popoverContentSize=CGSizeMake(100, 100);        //設定點擊時,popover不消失(預設是點擊popover外的任何地方就消失)//[self.menuPop setPassthroughViews:@[self.btn]];//[email protected][self.btn];

當然,最實用的就是上面的那個設定大小的屬性,但是如果設定大小需要在內容視圖控制器中設定呢?就不能實用setPopoverContentSize,需要用到如下的屬性:

self.preferredContentSize=CGSizeMake(100, 100);

(4)解決方案:-[UIPopoverController dealloc] reached while popover is still visible.

原因:建立的popover控制器是局部變數,一個花括弧執行完畢{}就銷毀了,所以會出現問題。

解決辦法:把局部變數變成全域變數即可。定義如下,然後使用的時候用self.helloPop即可:

@interface ViewController ()@property(nonatomic,strong) UIPopoverController *helloPop;@end

注意:在iOS8中已經不會出現這個錯誤,即只是在花括弧中定義的局部變數,但是不會報錯。這個錯誤一般是在iOS7及以下系統。


【iOS開發-106】UIPopoverController,[UIPopoverController dealloc] reached while popover is still visible

聯繫我們

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