2

I have dynamic tableview for which I have 6 cells.

Problem statement:

Now, I want to show a popover which should come just right to the selected cell and with arrow pointing left towards the cell .

What I did :

1) created the PopUpViewController.m/.h .
2) created a "Present as popover" segue from the cell to this popUpViewController.
3) currently anchor point in storyboard is set to tableview so the popover shows from the top left corner.

After some reading,

1) I created a XTableViewCell.m/.h class for the cell.
2) created a small button towards right end in the cell and put an outlet of it in XTableViewCell.h file to use this button as an anchor.

MY question is , I have done all this in storyboard.

Consider my tableViewController class as YTableViewController.h/.m

1) Where and how should I use the anchor button to anchor my popover to my requirement ?

2) By default, in story board the anchor is set to the cell name . Will it be overwritten to my button ?

3) Where should I configure the UIPopoverPresentationController params ?

4) How can show the popup as "popup" in iPhone also and not as full screen view controller?

Please help.

Adrian
  • 16,233
  • 18
  • 112
  • 180
CodeTry
  • 312
  • 1
  • 19

1 Answers1

1

First, you should create a popover segue from your view controller with your table view in it. You can do this by control dragging from the yellow view controller icon in the document outline to the view controller you want to popover. Give it a segue identifier, by clicking on the segue and opening the attributes inspector and adding it to the segue identifier field. Something like "Popover" will work for now.

Next, you should drag from the anchor view circle below the segue identifier field to your view controller's view (we'll adjust this later in code).

You should add a button to your UITableViewCell subclass. Then in your cellForRowAtIndexPath: you can add a target for the button like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellIdentifier = [menuItems objectAtIndex:indexPath.row]; // I'm not sure what's going on here but hopefully you do
    RegisterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    [cell.PopoverAnchorButton addTarget:self action:@selector(presentPopover:) forControlEvents:UIControlEventTouchUpInside];

    // Configure the cell...
    return cell;

}

Add the presentPopover: method to your view controller:

- (void) presentPopover:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"Popover" sender:sender];
}

Now, you can change the source view in your view controller's prepareForSegue:

You can get a reference to the UIPopoverPresentationController and check if the source view is an instance of your subclass of UITableViewCell. If so, adjust the source view to its button.

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Popover"] && segue.destinationViewController.popoverPresentationController) {
        UIPopoverPresentationController *popController = segue.destinationViewController.popoverPresentationController;
        popController.sourceView = sender;
        popController.delegate = self;
        // if you need to pass data you can access the index path for the cell the button was pressed by saying the following
        CGPoint location = [self.tableView convertPoint:sender.bounds.origin fromView:sender];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        // do something with the indexPath
    }
}

If you want the view controller to always be displayed as a popover even on implement the UIPopoverPresentationControllerDelegate method:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationStyleNone;
}

At this point, you'll likely have a warning. This can by silenced by telling the complier that you conform to the UIPopoverPresentationControllerDelegate protocol. For example:

@interface MyTableViewController () <UITableViewDataSource, UITableViewDelegate,UIPopoverPresentationControllerDelegate>
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • For some reasons , i am getting the below error before trying your answer Main.storyboard: Couldn't compile connection: property=anchorView destination=> wen i set the anchor point to button in the storyboard , i get the above error. I have already subclassed my cell and given the class name to the cell in the storyboard . – CodeTry Jun 06 '16 at 09:43
  • hi beyowulf, thankyou for the detailed explanation . I had written more or less the same code , but was getting the above error . Followed what u said and let the anchor point to Tableview itself and later changed in prepareForSegue. with your implementation , my popup is starting from the leftmost edge of the cell even wen my button is at the right end. Any inputs to do the same ? – CodeTry Jun 06 '16 at 16:52
  • Post code for adding a target to your button and the code for that action. Also, you could set a breakpoint or log what type sender is in `prepareForSegue`, if it's not a `UIButton` something not set up right. – beyowulf Jun 06 '16 at 17:03
  • Hi , i have put my code here , with another problem . http://stackoverflow.com/questions/37663578/popover-in-uitableviewcell-not-coming-immediately I guess , i will switch to static table cells :( – CodeTry Jun 06 '16 at 17:36
  • hi , i just checked . you are write .code for button action is not invoked. m checking more . this is the benefit of working with professionals ! . upvoted your answer. – CodeTry Jun 06 '16 at 17:43
  • I updated my answer. You have to set the button's target for the cell that you are returning from `cellForRowAtIndexPath` – beyowulf Jun 06 '16 at 19:11
  • Hey @ beyowulf that is not work as you said .i have get a error like Main.storyboard: Couldn't compile connection: property=anchorView destination=> – Hiren kanetiya Jan 05 '18 at 09:29
  • @Hirenkanetiya You are getting that because you've attached some outlet to a prototype cell. You can see the answer here: https://stackoverflow.com/a/9236869/5442445 for more explanation. My answer proposes that you _…should drag from the anchor view circle below the segue identifier field to your view controller's view_ And adjust it to the cell or cell subview in code. – beyowulf Jan 05 '18 at 15:29