4
votes

This an assignment of Stanford iPhone programming course. The aim of this program is to browse the photos in tableViewControllers and display them in an ImageViewController.

The original code is written in iOS 7 environment, so it had two storyboards for iPhone and iPad. The former uses a tabViewController, and the latter uses a splitViewController.

Since iOS 8 allows us to use the splitViewController in both iPhone and iPad, so I just delete the iPhone storyboard, and use the iPad one as universal layout. My storyboard layout is like this: Oops, I can't post the layout image here, not enough reputation. click here to see the storyboard

I set the two segues which link two tableViewController with the lowest navigationViewController to "Show detail". And same as in Xcode template "Master-detail application", I set the splitViewController to delegate itself, and the leftBarButtonItem of my detailViewController(ImageViewController) to displayModeButtonItem in appDelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
    splitViewController.delegate = self;
    return YES;
}

The problem is in iPhone portrait mode, the "Master" back button doesn't appear at the left-upper corner of the ImageViewController. In iPad mode, everything works fine.

I link my code here: code for this program. Hope someone could help. Thanks!

2

2 Answers

1
votes

I used unwind segue technique (programatically) with customised left bar button on the ImageViewController (i.e. the detail view).

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // Need a back button to appear when not in split view i.e. iPhone mode
    // Wire up to unwind segue action
    if (!self.splitViewController) {
        UIImage *image = [UIImage imageNamed:@"ImageViewControllerBarBackIndicatorDefault"];
        UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                initWithImage:image
                                style:UIBarButtonItemStylePlain
                                target:self
                                action:@selector(OnClick_btnBack:)];
        self.navigationItem.leftBarButtonItem = btnBack;
    }
}

-(IBAction)OnClick_btnBack:(id)sender {
    [self performSegueWithIdentifier:@"unwind" sender:self];
}

#pragma Unwind Segue

- (BOOL)canPerformUnwindSegueAction:(SEL)action
             fromViewController:(UIViewController *)fromViewController
                     withSender:(id)sender {
    return YES;
}

- (IBAction)unwindFromImageViewController:(UIStoryboardSegue *)unwindSegue {
    [self dismissViewControllerAnimated:YES completion:nil];
}
0
votes

I had the exact same problem. I followed the tutorial from here http://nshipster.com/uisplitviewcontroller/ but it's still not working. Not sure what the problem is..

I ended up doing the switch programmatically. It's not the most elegant solution but it's working.

if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
    self.navigationController?.pushViewController(settingsViewController, animated: true)
} else {
    self.detailViewController?.setViewControllers([settingsViewController], animated: false)
}