1

我知道這是一個老派的問題 - 但我確實在網上搜索過,並發現不推薦使用的解決方案。我將如何實現POPOver UIAlertcontroller作爲popOver(帶箭頭方向向上)barButton。這裏的代碼:如何在UIBarbutton中實現UIAlertcontroller作爲彈出窗口(箭頭方向向上)

- (IBAction)eventSortingAction:(UIBarButtonItem *)sender { 

UIAlertController * view= [UIAlertController 
         alertControllerWithTitle:@"My Title" 
         message:@"Select you Choice" 
         preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* ok = [UIAlertAction 
       actionWithTitle:@"OK" 
       style:UIAlertActionStyleDefault 
       handler:^(UIAlertAction * action) { 
        //Do some thing here 
        [view dismissViewControllerAnimated:YES completion:nil]; 

       }]; 
UIAlertAction* cancel = [UIAlertAction 
        actionWithTitle:@"Cancel" 
        style:UIAlertActionStyleCancel 
        handler:^(UIAlertAction * action) { 
         [view dismissViewControllerAnimated:YES completion:nil]; 

        }]; 
[view addAction:ok]; 
[view addAction:cancel]; 
[view setModalPresentationStyle:UIModalPresentationPopover]; 
view.modalInPopover = YES; 
view.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; 
view.popoverPresentationController.delegate = self; 
[self presentViewController:view animated:YES completion:nil]; 

UIView* senderView = [sender valueForKey:@"view"]; //HACK 
UIPopoverPresentationController* popover = view.popoverPresentationController; 
if (popover) { 
    popover.sourceView = senderView; 
    popover.sourceRect = senderView.bounds; 
    popover.permittedArrowDirections = UIPopoverArrowDirectionUp; 
    popover.barButtonItem = self.actionBarButton; 
    popover.delegate = self; 
}} 

顯然我總是得到一個「popover =無」。請幫忙!提前致謝!

順便說一句,這段代碼不是我的,只是在Xcode中測試它。

+0

用實際創建'UIAlertController'的代碼更新你的問題。並解釋你發佈的代碼與你想要的代碼實際發生了什麼。 – rmaddy

+0

嗨rmaddy我發佈了下面更新的問題。我無法編輯我的問題 - 仍然缺乏聲譽。 – macky12345

+0

確定你可以編輯。點擊您的問題下的修改鏈接。不要發佈答案。 – rmaddy

回答

0
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil 
                     message:nil 
                    preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction *actnCamera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action) { 
                }]; 

UIAlertAction *actnLibrary = [UIAlertAction actionWithTitle:@"Library" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action) { 
                }]; 

[alertController addAction:actnLibrary]; 
[alertController addAction:actnCamera]; 
[alertController setModalPresentationStyle:UIModalPresentationPopover]; 
UIPopoverPresentationController *popPresenter = [alertController 
               popoverPresentationController]; 
popPresenter.sourceView = self.view; 
CGRect frame = self.navigationController.navigationBar.frame; 
frame.origin.x = self.navigationItem.leftBarButtonItem.width; 
popPresenter.sourceRect = frame; 
popPresenter.barButtonItem = self.navigationItem.leftBarButtonItem; 
[self presentViewController:alertController animated:YES completion:nil]; 

輸出

enter image description here

+0

嗨PKT它不工作,它只顯示alertcontroller,當我點擊uibarbutton。我想要做的是,當我點擊uibarbutton是這樣的東西http://stackoverflow.com/questions/25805608/present-a-uialertcontroller-from-within-a-popover-in-ios8 – macky12345

+0

@ macky12345請參閱編輯 –

+0

即時消息模擬iPhone中,仍然不能實現這一點。這隻能在iPad上完成嗎?或者我只是在實施這個錯誤。我已經複製並粘貼了你的代碼。 – macky12345

2

只是一個提醒,因爲這是在谷歌頂部結果:

popPresenter.barButtonItem是popPresenter.sourceView + popPresenter.sourceRect

替代

See(source)

關於OP問題,應使用IBAction參數sender

UIPopoverPresentationController *popPresenter = [alertController 
              popoverPresentationController]; 
popPresenter.barButtonItem = sender; 
[self presentViewController:alertController animated:YES completion:nil];