0

I have a problem like this: I have a root view which has a UIPopoverController and a button, when I click on that button, it present a tableView. My app display OK but now when I click in any row, the UIPopoverController still display and I want it dismiss.

Any help? Thanks!

This is my code:

ViewController.h

#import <UIKit/UIKit.h>
#import "UIPopoverIphone.h"
#import "TableViewController.h"

@interface ViewController : UIViewController <UIPopoverControllerDelegate, TableViewPopoverDelegate> {
    TableViewController *popoverView;
    IBOutlet UIButton *popButton;
    UIPopoverController *pop;
}

@property (strong, nonatomic) UIPopoverController *pop;

+(void)hidePop;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize pop;

-(void)dealloc {
    [pop release];
    [super dealloc];
}

-(IBAction)showPop {
    popoverView = [[TableViewController alloc] init];
    popoverView.delegate = self;
    pop = [[UIPopoverController alloc] initWithContentViewController:popoverView];
    [pop setPopoverContentSize:CGSizeMake(100, 200)];
    [pop setDelegate:self];
    [pop presentPopoverFromRect:popButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}


-(void)hidePop {
    [pop dismissPopoverAnimated:YES];
}

....

@end

TableViewController.h

#import <UIKit/UIKit.h>

@protocol TableViewPopoverDelegate <NSObject>

-(void) dismissPopover;

@end

@interface TableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *itemsArray;
    UITableView *tableView;
    id<TableViewPopoverDelegate> delegate;
}

@property (nonatomic, retain) NSArray *itemsArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) id<TableViewPopoverDelegate> delegate;

@end

TableViewController.m

#import "TableViewController.h"

@implementation TableViewController


@synthesize itemsArray;
@synthesize tableView;


-(void) dealloc {
    [itemsArray release];
    [tableView release];
    [super dealloc];
}

-(void)loadView {
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
    self.view = mainView;
    [mainView release];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 200) style:UITableViewStylePlain];
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

....

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    NSLog(@"%@",[self.itemsArray objectAtIndex:indexPath.row]);
    [self.delegate dismissPopover];
}

@end
o0oKodako0o
  • 177
  • 3
  • 9

3 Answers3

1

It appears that your root view ViewController doesn't correctly implement your TableViewPopoverDelegate. You've declared a method in that delegate called dismissPopover, but you haven't implemented it in ViewController.m.

In ViewController.m, change the hidePop function to:

-(void)dismissPopover {
    [pop dismissPopoverAnimated:YES];
}
pdriegen
  • 2,019
  • 1
  • 13
  • 19
1

With your current work flow, the ViewController have no connection with TableViewController. It looks like you are presenting the UITableViewController from the UIPopoverIphone not from the ViewController. So the delegate callback method dismissPopover needed to be implemented within the UIPopoverIphone class not the ViewController class. If you still want to dismiss the UIPopoverIphone from the ViewController, there are two options:

  1. Using NSNotificationCenter: In the TableViewController, you can post a notification when you are ready to dismiss the popover. Then in the ViewController, you can observer that notification and dismiss the popover. In this scenario, no delegate is involved.

  2. Setup the delegate callback from your UIPopoverIphone class and the ViewController class. So when you tap the button in the popover, it makes a callback to ViewController, you then dismiss the popover and prepare your TableViewController and present it. Here your TableViewController and the ViewController have a connection.

user523234
  • 14,323
  • 10
  • 62
  • 102
0

Popovers by default dismiss themselves when a tap is detected outside of their bounds.

deleted_user
  • 3,817
  • 1
  • 18
  • 27