2

我正在使用 XCode 4.3.2 版创建 iPad 应用程序。我无法弄清楚如何关闭在情节提要中创建的弹出框。

在我的主屏幕上,我有一个按钮。在情节提要上,我从那个按钮到我的弹出框定义了一个segue。我的弹出框是一个表格视图控制器。在弹出框表视图中选择一个项目后,我将所选信息发送回父级并尝试关闭弹出框。一切正常,除了我无法关闭弹出窗口。

主屏幕 .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

感谢您的任何帮助或指导。

4

2 回答 2

3

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

@property (nonatomic, strong) UIPopoverController *sortByPopoverController;

然后,在启动弹出窗口时,我包括了这个:

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

包含该代码允许我在委托回叫时正确关闭弹出框:

[self.sortByPopoverController dismissPopoverAnimated:YES];
于 2012-05-16T18:45:44.870 回答
0

很快就叫这个

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

  }
于 2015-08-17T08:49:59.063 回答