How to call PopOver Controller from UITableViewCell.accessoryView?

First, I would like to say that I am really new to the development of ipad / ipod / iphone, as well as to objective-c.

With that said, I'm trying to develop a small iPad-oriented application using Xcode and IB, basically, I have a table for each UITableViewCell in the table, I added a button containing an image to the accessory.

Here is the code:

UIImage *img = [UIImage imageNamed:@"myimage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
button.frame = frame;   // match the button size with the image size

[button setBackgroundImage:img forState:UIControlStateNormal];

// set the button target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

So far, so good, now the problem is that I want the PopOver control to appear when the user clicks a button on a cell accessory.

I tried this on the "accessoryButtonTappedForRowWithIndexPath" table View:

UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;

//customViewController is the controller of the view that I want to be displayed by the PopOver controller
customViewController = [[CustomViewController alloc]init];

popOverController = [[UIPopoverController alloc]
initWithContentViewController: customViewController];

popOverController.popoverContentSize = CGSizeMake(147, 122);

CGRect rect = button.frame;

[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

The problem with this code is that it shows a Popover at the top of the View application, and when debugging, I saw the values ​​"rect", and they:

x = 267
y = 13

, , PopOver , : PopOver, ?

, , "cell.accessoryView" "inView:", ?

+3
3

button.bounds button.frame, rect inView.

, , popover , . , Any.

+8
[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

inView: cell.accessoryView inView: cell

+1

here for quick:

let cell = tableView.cellForRowAtIndexPath(indexPath)
let rect = tableView.convertRect(tableView.rectForRowAtIndexPath(indexPath), toView: tableView)


    var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("loadZone") as! UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)

    nav.modalPresentationStyle = UIModalPresentationStyle.Popover

    var popover = nav.popoverPresentationController

    popoverContent.preferredContentSize = CGSizeMake(100,200)
    popover!.sourceView = tableView
    popover!.sourceRect = rect

    self.presentViewController(nav, animated: true, completion: nil)

enter image description here

+1
source

Source: https://habr.com/ru/post/1745195/


All Articles