1
votes

I am learning objective C. Now I am curios with UISplitViewController and show/hide master view button. I have seen a lot of tutorial but I do something wrong and I don't know what is it. My button doesn't hide master view. I absolutely broke my mind in two days. Now I built this sample project to ask your advice. In my pattern I have: TabBarVC, SplitVC, TableViewVC and ViewVC. All controllers are my custom. MyTabBarViewController is coming first. I has the tab with UISplitViewController.

MySplitViewController.m

#import "MySplitViewController.h"
#import "MyTabBarViewController.h"
#import "MyDetailViewController.h"

@interface MySplitViewController ()

@end

@implementation MySplitViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    MyTabBarViewController *masterVC = [self.storyboard instantiateViewControllerWithIdentifier:@"myTableVC"];
    MyDetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"myDetailVC"];

    NSArray *newViewControllersArray = [NSArray arrayWithObjects:masterVC, detailVC, nil];

    self.viewControllers = newViewControllersArray;

}

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


@end

MyTableViewController.m

#import "MyTableViewController.h"

@interface MyTableViewController ()

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.dataArray = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
}

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

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

//configure the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];

    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];

    return cell;
}

@end

MyDetailViewController.h

#import <UIKit/UIKit.h>

@interface MyDetailViewController : UIViewController <UISplitViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *showHideMasterVC;
@property (nonatomic, strong) UIPopoverController *popover;

@end

MyDetailViewController.m

#import "MyDetailViewController.h"

@interface MyDetailViewController ()

@end

@implementation MyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)splitViewController:(UISplitViewController *)svc
    willHideViewController:(UIViewController *)aViewController
         withBarButtonItem:(UIBarButtonItem *)barButtonItem
      forPopoverController:(UIPopoverController *)pc
{
    //Grab a reference to the popover
    self.popover = pc;
}

-(void)splitViewController:(UISplitViewController *)svc
    willShowViewController:(UIViewController *)aViewController
 invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    //Nil out the pointer to the popover.
    _popover = nil;
}

@end

Here's my whole project with storyboard: MyProject

Sorry for a lot of code, but please help me or I get crazy. Thank you! PS By the way is it possible to have show/hide master view button outside NavigationController, on some view for example?

And now I got that - (void)splitViewController(UISplitViewController *)svc willHideViewController and - (void)splitViewController(UISplitViewController *)svc willShowViewController is deprecated in iOS8.

So can somebody correct my project what I should do for solving my problem?

1

1 Answers

3
votes

I think the simplest way is to add self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; in your MyDetailViewController viewDidLoad. This will show the show/hide MasterDetailViewController button.

And your - (void)splitViewController(UISplitViewController *)svc willHideViewController and - (void)splitViewController(UISplitViewController *)svc willShowViewController is deprecated in iOS8.

I had a similar problem, and for me, I solved it by simply creating my project with Master-Detail Application template. It comes with show/button and - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController as a default. If you are using a TabBarViewController as a rootViewController then you might want to set that as the rootViewController in applicationDidFinishLaunchingWithOptions. Hope this will help solve your problem.

Edit:

PS By the way is it possible to have show/hide master view button outside NavigationController, on some view for example?

First create a UIButton in your storyboard. Connect it to IBAction.

- (IBAction)buttonAction:(id)sender {

self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
[self.splitViewController.displayModeButtonItem action];
}

This should work.