2013-08-16 112 views
1

當UIButton被點擊以顯示UIPopovercontroller時,它顯示在另一個UIbarbuttonitemand上並顯示空白的黑色不透明UIPopovercontroller。UIPopoverController顯示在另一個按鈕上

這裏是我的實現代碼:

- (IBAction)buttonTapped:(id)sender { 

UIButton *btnAction; 

BookmarkViewController* bookmarkVC = [[BookmarkViewController alloc] init]; 

_buttonPopoverController = [[UIPopoverController alloc] 
           initWithContentViewController:bookmarkVC]; 

_buttonPopoverController.delegate = self; 

CGRect popoverFrame = btnAction.frame; 
[_buttonPopoverController setPopoverContentSize:CGSizeMake(320, 355) animated:NO]; 

//only required if using delegate methods 

[_buttonPopoverController presentPopoverFromRect:popoverFrame 
               inView:self.view 
          permittedArrowDirections:UIPopoverArrowDirectionUp 
              animated:YES]; 
} 
+0

你是如何設置'UIButton的* btnAction'到正確的按鈕? – Alexander

+0

使用故事板連接buttonTapped – user1120133

+1

那可能是你的問題了。您需要根據提供給函數的'(id)sender'的某些屬性來選擇按鈕,或者只需將發送者分配給按鈕:'* btnAction = sender' – Alexander

回答

1

你缺少assing senderbtnAction

UIButton *btnAction=(UIButton *)sender;

試試這個,

- (IBAction)buttonTapped:(id)sender { 

UIButton *btnAction=(UIButton *)sender; 


UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
UIViewController *bookmarkVC = [storyboard instantiateViewControllerWithIdentifier:@"BookmarkViewController"]; 

_buttonPopoverController = [[UIPopoverController alloc] 
           initWithContentViewController:bookmarkVC]; 

_buttonPopoverController.delegate = self; 

CGRect popoverFrame = btnAction.frame; 
[bookmarkVC setContentSizeForViewInPopover:CGSizeMake(320, 355)]; 

//only required if using delegate methods 

[_buttonPopoverController presentPopoverFromRect:popoverFrame 
               inView:self.view 
          permittedArrowDirections:UIPopoverArrowDirectionUp 
              animated:YES]; 
} 
+0

現在它顯示在正確的uibutton下,但爲什麼它顯示空白。它應該顯示添加,編輯和完成按鈕的表格視圖。 – user1120133

+0

檢查更新回答: BookmarkViewController * bookmarkVC = [[BookmarkViewController alloc] initWithNibName:@「BookmarkViewController」bundle:nil];' –

+0

當uibutton被點擊以顯示uipopovercontroller時,此更改崩潰app – user1120133

1

你的問題就在這裏

[_buttonPopoverController presentPopoverFromRect:popoverFrame 
               inView:self.view 
          permittedArrowDirections:UIPopoverArrowDirectionUp 
              animated:YES]; 

其中presentPopoverFromRect:popoverFrame應該是真正的酥料餅的RECT沒有UIButton,也是的btnAction沒有框架,你還沒有分配它,你是執行CGRect popoverFrame = btnAction.frame;

應該是這樣

[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 300.0, 50.0) inView:self.view 
       permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 

希望這有助於。

+0

根據[文檔:](http ://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverController_class/Reference/Reference.html#//apple_ref/occ/instm/UIPopoverController/presentPopoverFromRect:inView:permittedArrowDirections:animated :)'rect:The 「 – Alexander

+0

我試過你的更改但不工作 – user1120133

+0

你改變了什麼...... – IronManGill

相關問題