在表视图中选择项目后无法关闭popover

时间:2012-05-15 19:05:39

标签: ios5 uitableview uipopovercontroller xcode4.3

我正在使用XCode版本4.3.2创建和iPad应用程序。我无法弄清楚如何关闭在故事板中创建的弹出窗口。

在我的主屏幕上,我有一个按钮。在故事板上,我有一个从该按钮定义到我的popover的segue。我的popover是一个表视图控制器。在弹出窗口视图中选择一个项目后,我将所选信息发送回父级并尝试关闭弹出窗口。一切正常,除非我不能让popover关闭。

主屏幕.m文件的代码:

#import "SectionViewController.h"
#import "SortByTableViewController.h"

@interface SectionViewController () <SortByTableViewControllerDelegate>
@end

@implementation SectionViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"DisplaySortByOptions"]) 
    {
        SortByTableViewController *popup = (SortByTableViewController*)segue.destinationViewController;
        popup.selectedSection = self.selectedSection;
        popup.receivedOption = self.selectedItemCharacteristic;
        popup.delegate = self;
    }
}

- (void)sortByTableViewController:(SortByTableViewController *)sender 
                           returnedOption:(ItemCharacteristic *)returnedOption
{
    if(!returnedOption)
    {
        [self.sortByButton setTitle:@"SHOW ALL" forState:UIControlStateNormal]; 
    }
    else 
    {
        [self.sortByButton setTitle:returnedOption.name forState:UIControlStateNormal];
    }
    self.itemCharacteristic = returnedOption;
    [self dismissViewControllerAnimated:YES completion:nil]; //THIS DOES NOT CLOSE THE POPOVER
}

popover .h文件的代码:

#import <UIKit/UIKit.h>

@class SortByTableViewController;

@protocol SortByTableViewControllerDelegate <NSObject>

- (void)sortByTableViewController:(sortByTableViewController *)sender 
                           returnedOption:(ItemCharacteristic *)returnedOption;

@end

@interface SortByTableViewController : UITableViewController

@property (nonatomic, strong) Section *selectedSection;
@property (nonatomic, strong) ItemCharacteristic *receivedOption;
@property (nonatomic, weak) id <SortByTableViewControllerDelegate> delegate;

@end

popover .m文件的代码:

#import "SortByTableViewController.h"

@interface SortByTableViewController () <UITableViewDelegate>

@end

@implementation SortByTableViewController

@synthesize selectedSection = _selectedSection;
@synthesize receivedOption = _receivedOption;
@synthesize delegate = _delegate;

...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ItemCharacteristic *itemCharacteristic = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [self.delegate sortByTableViewController:self returnedOption:itemCharacteristic];
    [self dismissViewControllerAnimated:YES completion:nil]; //THIS DOESN'T WORK
    [self.navigationController popViewControllerAnimated:YES]; //THIS DOESN'T WORK EITHER
}

@end

感谢您提供任何帮助或指导。

2 个答案:

答案 0 :(得分:3)

我找到了答案。我必须将以下属性添加到我的主屏幕:

@property (nonatomic, strong) UIPopoverController *sortByPopoverController;

然后,在启动弹出窗口时,我将其包括在内:

UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
self.sortByPopoverController = popoverSegue.popoverController;

包含该代码允许我在委托回调时正确解除弹出窗口:

[self.sortByPopoverController dismissPopoverAnimated:YES];

答案 1 :(得分:0)

swift中的

只是打电话给这个

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)   
  {
    self.dismissViewControllerAnimated(true, completion: nil)

  }