1

I know that this is old school question - but I did searched the web and found solutions to be deprecated. How would I implement a UIAlertcontroller as popOver(with arrow direction up) in a barButton. Here's the code:

 - (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;
}}

apparently I always got a "popover = nil". Please Help! Thanks in advance!

By the way this code is not mine, just testing it in Xcode.

macky12345
  • 99
  • 1
  • 7
  • Update your question with the code that actually creates the `UIAlertController`. And explain what is actually happening with the code you posted versus what you want it to do. – rmaddy Apr 25 '16 at 02:18
  • Hi rmaddy i posted the updated question below. I cannot edit my question - still lacking Reputation. – macky12345 Apr 25 '16 at 03:12
  • Sure you can edit. Click the edit link under your question. Don't post an answer. – rmaddy Apr 25 '16 at 03:14
  • Thanks didn't able to see, there updated it. So what I really want is the have a alert msg that that anchors(has an arrow pointing to the uibarbutton) with the uibarbutton that i have. What happen actually is that it show the conventional alert controlle – macky12345 Apr 25 '16 at 03:20

2 Answers2

3

Just a reminded because this is a top result on Google :

popPresenter.barButtonItem is an alternative to popPresenter.sourceView+popPresenter.sourceRect

See (source)

Regarding OP question, IBAction parameter sender should be used.

UIPopoverPresentationController *popPresenter = [alertController
                                             popoverPresentationController];
popPresenter.barButtonItem = sender;
[self presentViewController:alertController animated:YES completion:nil];
FLK
  • 98
  • 2
  • 8
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];

OUTPUT

enter image description here

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • hi PKT it doesnt work, it just shows the alertcontroller when i clicked the uibarbutton. What i wanted to do is that when i click the uibarbutton is something like this http://stackoverflow.com/questions/25805608/present-a-uialertcontroller-from-within-a-popover-in-ios8 – macky12345 Apr 25 '16 at 05:18
  • Im simulating in iPhone and still I cant implement this. Can this be only done in iPad? Or Im just wrong in implementing this. I have copied and pasted your code. – macky12345 Apr 25 '16 at 06:11
  • for iphone create custom class that contains tableview and present it on your call as popover might solve your problem .... – Prashant Tukadiya Apr 25 '16 at 07:30
  • Don't just post code. Explain what was wrong and explain how your answer fixes the issue. – rmaddy Apr 25 '16 at 15:47