chrizonline
chrizonline

Reputation: 4979

ipad-how to open a popup view controller in a UITableViewCell

These are the files I am working on:

  1. ChecklistViewController.h
  2. ChecklistViewController.m
  3. ChecklistTableViewCell.h
  4. ChecklistTableViewCell.m
  5. ChecklistTableViewCell.xib
  6. DetailViewController.h
  7. DetailViewController.m
  8. DetailViewController.xib

I have a ChecklistViewController which displays a UITableView. Because I needed a different layout for the UITableViewCell, I have subclass UITableViewCell. I have created a nib file ChecklistTableViewCell.xib which has a UITableViewCell that contains a label, button and switch view. i have linked the UITableViewCell to a custom class ChecklistTableViewCell.

I have linked up the necessary outlets from the nib file to ChecklistTableViewCell class, and in ChecklistViewController cellForRowAtIndexPath I'm able to display label text.

In the UITableViewCell class file ChecklistTableViewCell, I have also linked and implemented a IBAction method that will be called when the button is clicked. When this method is called, how do I open DetailViewController as a popup?

Here is the snippet of my IBAction method:

-(IBAction)showPopup:(id)sender
{
    NSLog(@"popup");
    DetailViewController *vp = [[DetailViewController alloc] init];
    vp.modalPresentationStyle = UIModalPresentationFormSheet;
    vp.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    vp.view.superview.frame = CGRectMake(0, 0, 540, 620);
}

Upvotes: 0

Views: 1185

Answers (2)

Joemon Johny
Joemon Johny

Reputation: 56

If you want to show a detail view controller as pop up in iPad there is a control called UIPopoverController.

How to use UIPopoverController?

In .h file

    UIPopoverController *popoverController; 

In .m file

     DetailviewController * objViewController = [[DetailviewController alloc]initWithNibName:@"DetailviewController" bundle:nil];
     obj.delegate = (id )self; // if DetailViecontroller has any delegate.

     popoverController = [[UIPopoverController alloc] initWithContentViewController:objViewController];

     popoverController.popoverContentSize = CGSizeMake(320.0, 400.0);

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:theRow inSection:1]];
     CGRect rect=CGRectMake(cell.bounds.origin.x+600, cell.bounds.origin.y+10, 50, 30); 

    [popoverController presentPopoverFromRect:rect  inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Upvotes: 0

Allen Zeng
Allen Zeng

Reputation: 2665

rdelmar is spot on with his comment. Because presentViewController:animated:completion is a method in the view controller, your ChecklistTableViewCell needs to make the view controller aware of the button click.

You have 2 choices:

Assuming your data source is your view controller, in your view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChecklistTableViewCell *cell = ...;

    cell.button.tag = indexPath.row; // or whatever appropriate

    [cell.button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)buttonTapped:(UIButton *)button
{
    DetailViewController *vc = ...; // get your "popup" accordingly using button.tag
    [self presentViewController:vc animated:YES completion:nil];
}

Or you can declare a protocol that your view controller adheres to:

// ChecklistTableViewCell.m
@protocol ChecklistTableViewCellDelegate
- (void)buttonTapped:(ChecklistTableViewCell *)sender;
@end

@implementation ChecklistTableViewCell : UITableViewCell
{
    ...

    - (IBAction)showPopup:(id)sender // maybe change the name to something more appropriate
    {
        if ([self.delegate respondsToSelector:@selector(buttonTapped:)])
        {
            [self.delegate buttonTapped:self];
        }
    }

    ...
}

// ChecklistViewController.m
@implementation ChecklistViewController : ChecklistTableViewCellDelegate
{
    ...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ChecklistTableViewCell *cell = ...;

        cell.tag = indexPath.row; // or whatever appropriate

        cell.delegate = self;

        ...

        return cell;
    }

    - (void)buttonTapped:(ChecklistTableViewCell *)sender
    {
        DetailViewController *vc = ...; // get your "popup" accordingly using sender.tag
        [self presentViewController:vc animated:YES completion:nil];
    }

    ...
}

Upvotes: 1

Related Questions