MathewB83
MathewB83

Reputation: 23

UISplitViewController - detailView is not showing

In AppDelegate we create an DetailView, then a loginView is presented (UIModalPresentationFullScreen) on top of it. After logged in, loginView is dismissed.

DetailView is having a tableView and when you select a cell/row and 2nd detailView is pushed.

What I did so far: In AppDelegate I ask for UI_USER_INTERFACE_IDIOM() and when idiom is iPad i create a splitView:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    FirstDetailViewController* fdvc = [[FirstDetailViewController alloc] initWithNibName:@"FirstDetailViewController" bundle:nil];
    SecondDetailViewController* sdvc = [[SecondDetailViewController alloc] initWithNibName:@"SecondDetailViewController" bundle:nil];

    UINavigationController* fdvcNavigationController = [[UINavigationController alloc] initWithRootViewController:fdvc];
    UINavigationController* sdvcNavigationController = [[UINavigationController alloc] initWithRootViewController:sdvc];

    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.viewControllers = [NSArray arrayWithObjects:fdvcNavigationController, sdvcNavigationController, nil];

After login my LoginView is dismissed and my UISplitViewController is shown, with the 1st DetailView on the left side (master). So everything went fine here.

Now I go to the FirstDetailViewController.m and search for the didSelectRowAtIndexPath because there I find the pushView to SecondDetailViewController in the "iPhone version".

And here is where i am stuck. I have tried several SplitView tutorials and read problems of others regarding splitview.

But I think my problem is some kind of "basic" because I am new to programming / iOS in general and don't know all my tools.

Any help would be appreciated.

Upvotes: 2

Views: 1449

Answers (1)

jjv360
jjv360

Reputation: 4140

When writing apps with a table view and some other sort of "display" view for items in the table, I do this so it works on both devices:

// In App Delegate...
-(BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Create views
    MyTableViewController* myTable = [[MyTableViewController alloc] init];
    MyDetailViewController* myDetail = [[MyDetailViewController alloc] init];
    UINavigationController* tableNav = [[UINavigationController alloc] initWithRootViewController:myTable];
    UINavigationController* detailNav = [[UINavigationController alloc] initWithRootViewController:myDetail];

    // Check device type
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {

        // Use a split view
        UISplitViewController* splitView = [[UISplitViewController alloc] init];
        split.viewControllers = @[tableNav, detailNav];
        self.window.rootViewController = split;

    } else {

        // Use a single view for iPhone
        self.window.rootViewController = tableNav;

    }

}

.

// In table view
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Check device type
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {

        // Tell the already visible detail view to load something
        MyData* data = ...; \\ Get your thing to display however you want
        [[NSNotificationCenter defaultCenter] postNotificationName:@"DisplayMyData" object:data];

    } else {

        // No detail controller exists yet, so create it
        MyDetailViewController* myDetail = [[MyDetailViewController alloc] init];
        [self.navigationController pushViewController:myDetail animated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"DisplayMyData" object:data];

    }

}

.

// In detail view
-(void) viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayData:) name:@"DisplayMyData" object:nil];

}

-(void) displayData:(NSNotification*)notify {

    MyData* data = (MyData*) notify.object;

    ... // Display however...

}

Upvotes: 3

Related Questions