我有这样的问题:我有一个根视图,它有一个 UIPopoverController 和一个按钮,当我单击该按钮时,它会显示一个 tableView。我的应用程序显示正常,但现在当我单击任何行时,UIPopoverController 仍然显示,我希望它关闭。
有什么帮助吗?谢谢!
这是我的代码:
视图控制器.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
视图控制器.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
表视图控制器.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
表视图控制器.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