4

I am new to iOS development and need a bit of help. I have searched high and low and cannot find the answer. It's more like I don't understand what is happening. My issue is, I am trying to code a Master-Detail App without using storyboards. This helps me get a better understanding of how ViewControllers are managed and handled. I am trying to present the DetailViewController after an item is selected in a tableView in the MasterViewController on iPhone. On iPad it works well because both views are side by side.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Pass data to detailViewController
    if ([_detailDelegate respondsToSelector:@selector(item:selectedAtIndexPath:)]) {

            [_detailDelegate item:[[BNRItemStore sharedStore] findItemAtIndex:indexPath.row] selectedAtIndexPath:indexPath];
    }

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {

        // show DetailViewController


        NSArray *viewControllers = self.splitViewController.viewControllers;

        UINavigationController *detailNavigationViewController = [viewControllers objectAtIndex:1];

        [self showDetailViewController:detailNavigationViewController sender:nil];

    }


}

In a storyboard the selected ViewCell has a show detail segue to the detailViewController. How can I achieve the same thing without using storyboards?

My AppDelegate code for a better picture

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

UISplitViewController *splitViewController = [[UISplitViewController alloc] init];

self.window.rootViewController = splitViewController;


UINavigationController *masterNavigationController = [[UINavigationController alloc]init];
UINavigationController *detailNavigationController = [[UINavigationController alloc]init];


MasterTableViewController *masterTableViewController = [[MasterTableViewController alloc] init];
DetailViewController *detailViewController = [[DetailViewController alloc] init];

masterTableViewController.detailDelegate = detailViewController;
detailViewController.masterDelegate = masterTableViewController;


masterNavigationController.viewControllers = @[masterTableViewController];
detailNavigationController.viewControllers = @[detailViewController];

[splitViewController setViewControllers:@[masterNavigationController,detailNavigationController]];


[self.window makeKeyAndVisible];

    return YES;
}

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
leafartist
  • 613
  • 1
  • 5
  • 12

2 Answers2

4

I was able to solve this issue after reading this post. When the UISplitViewController detects that it is in portrait mode on an iPhone it removes the DetailViewController from its viewControllers property. When it is in landscape mode its add the DetailViewController(Secondary Controller)back to the viewControllers property.

Solution:

Added a strong reference property in the MasterViewController(Primary Controller) to store the DetailViewController.

When an item is selected in the MasterViewController table, I checked the UISplitViewController viewControllers property and if the DetailViewController was missing from the Array, I pushed the DetailViewController onto the Parent Controller(UINavigationViewController)stack.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

DetailViewController *detailController = (DetailViewController *) self.detailViewController.viewControllers[0];

[detailController item:[[BNRItemStore sharedStore] findItemAtIndex:indexPath.row] selectedAtIndexPath:indexPath];

if (self.splitViewController.viewControllers.count == 1) {

    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

}
Community
  • 1
  • 1
leafartist
  • 613
  • 1
  • 5
  • 12
2

Try changing:

    [self showDetailViewController:detailNavigationViewController sender:nil];

to:

    [self.splitViewController showDetailViewController:detailNavigationViewController sender:nil];
beyowulf
  • 15,101
  • 2
  • 34
  • 40