如何隐藏&取消隐藏SplitView控制器中的主视图控制器

时间:2013-09-16 11:51:07

标签: ios objective-c

我有一个Master Detail视图控制器,我正在尝试编写显示或隐藏Master列表的方法/消息。我已经按照this almost identical question的所有答案,但它们都不适合我。我正在为iOS6开发,但即使在遵循解决这个问题的答案的建议之后,我仍然会得到无用的SIG_ABRT错误。

这是我的代码:

WWFMasterViewController.h:

#import <UIKit/UIKit.h>
@class WWFDetailViewController;
@interface WWFMasterViewController : UITableViewController
@property (strong, nonatomic) WWFDetailViewController *detailViewController;
@end

WWFMasterViewController.m:

#import "WWFMasterViewController.h"
#import "WWFDetailViewController.h"

@interface WWFMasterViewController ()
@property (nonatomic,strong) NSArray * arr;
@end

@implementation WWFMasterViewController
@synthesize arr;

- (void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    }
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    arr=@[@"First",@"Second",@"Third",@"Fourth",@"Fifth"];
    self.detailViewController = (WWFDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = arr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.detailViewController.detailItem = arr[indexPath.row];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        [[segue destinationViewController] setDetailItem:arr[indexPath.row]];
    }
}

@end

WWFDetailViewController.h:

#import <UIKit/UIKit.h>
@interface WWFDetailViewController : UIViewController <UISplitViewControllerDelegate>
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end

WWFDetailViewController.m:

#import "WWFDetailViewController.h"

@interface WWFDetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation WWFDetailViewController

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

- (BOOL) splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
    return YES;
}

@end

如何为显示/隐藏主列表的代码编写方法/消息?

TIA

2 个答案:

答案 0 :(得分:2)

Use following method to hide the split view:

.h file must contain UISplitViewControllerDelegate

// In split delegate
- (void)hideMaster {

    [self.splitViewController.view setNeedsLayout];
    self.splitViewController.delegate = nil;
    self.splitViewController.delegate = self;

    [self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];

}// end hideMaster


-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{

    return YES;

}

Use following method to show the split view:

// In split delegate
- (void)hideMaster {

    [self.splitViewController.view setNeedsLayout];
    self.splitViewController.delegate = nil;
    self.splitViewController.delegate = self;

    [self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];

}// end hideMaster


-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{

    return NO; // Important when show the split view again

}

// vKj

答案 1 :(得分:1)

好的,我发现了如何在iOS6中隐藏主视图控制器。这是我使用的代码:

[self.masterPopoverController dismissPopoverAnimated:YES];

为了显示主视图控制器,我认为需要这个函数presentPopoverFromBarButtonItem:permittedArrowDirections:animated:,但我还没有测试过。

此代码进入DetailViewController。