1

我有一个主细节视图控制器,我正在尝试编写一个显示或隐藏主列表的方法/消息。我已经遵循了这个几乎相同问题的所有答案,但没有一个对我有用。我正在为 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

4

2 回答 2

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

于 2013-12-02T10:52:22.307 回答
1

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

[self.masterPopoverController dismissPopoverAnimated:YES];

为了显示主视图控制器,我认为需要此功能,presentPopoverFromBarButtonItem:permittedArrowDirections:animated:但我尚未对此进行测试。

此代码进入 DetailViewController。

于 2013-09-17T11:47:56.820 回答