Question

I want to pass some core data from master to detail.

First the user selects a name which has a gender assigned. If gender Girl "pige" it goes to the girls UITableiew. From there each selection shows on detail view. I want to pass some data from the girl UITableiew. Like if its a girl color it pink. set a title etc. So far i know how to pass around the UITableiew "master" but not how to get it to the detail view

Here's some code on how i manage the selection of UITableView etc.

DetailViewContainerController

 UIStoryboard* initial     = [UIStoryboard storyboardWithName:@"Main-iPad" bundle:nil];
self.initital              = [initial instantiateViewControllerWithIdentifier:@"initialSite"];
[self addChildViewController:self.initital];
self.startSide              = [initial instantiateViewControllerWithIdentifier:@"startSide"];
[self addChildViewController:self.startSide];

-(void)initialSite:(int)viewId 
{
    UIViewController *viewController;
    switch (viewId) 
    {
        case 0:
            viewController = self.initital;
            [self.navigationController setNavigationBarHidden:NO animated:NO];
            break;
        case 1:
            viewController = self.startSide;
            [self.navigationController setNavigationBarHidden:NO animated:NO];
            break;
    }
    [self showChildViewController:viewController];
}

-(void)showChildViewController:(UIViewController*)content 
{
    if(topController != content) 
    {
        content.view.frame = [self.view frame]; // 2
        [self.view addSubview:content.view];
        [content didMoveToParentViewController:self];
        topController = content;
    }
}

startTableViewController

-(void)viewDidLoad 
{
    arrayA = [[[NSMutableArray alloc] initWithArray:[Start findAllSortedBy:@"navn" ascending:YES]] mutableCopy];
}

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

    if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"gender"]isEqualToString:@"Pige"]) 
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main-iPad" bundle:nil];
        DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:@"MenuPige"];
        controller.forNavn = [arrayA objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:controller animated:YES];
    }

    if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"gender"]isEqualToString:@"Dreng"]) 
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main-iPad" bundle:nil];
        DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:@"MenuDreng"];
        controller.forNavn = [arrayA objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

MenuPige

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.detailViewContainerController initialSite:[indexPath row]];

if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"menuPunkt"]isEqualToString:@"Barnedåb / Navngiving"]) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"BarneDaab" bundle:nil];
    DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:@"barneDaab"];
    [self.navigationController pushViewController:controller animated:YES];

}

in the girl menu.h i have the NSStrings of the data that i want to pass. So that is working. but how to get it to detailviewcontroller ?

UPDATE: I tried this Accessing array from a detail view controller into master view controller in Objective-C

but can't get it work

UPDATE 2:

    self.navigationController.parentViewController.navigationItem.title =     [self.detailViewContainerController.forNavn valueForKey:@"navn"];
doesn't work in the velkommen.m, it returns nil

UPDATE 3:

Leftmenucontroller.m - this works. but only to another uitableview. can't get it over to the detail view
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewContainerController *start = [[DetailViewContainerController alloc]init];
    start.forNavn = forNavn;
    NSLog(@"leftview = %@", forNavn);
    [self.detailViewContainerController initialSite:[indexPath row]];
}

UPDATE 4:

startTableViewController.h

{
    NSMutableArray      *arrayA;
    NSMutableArray      *tableData;
    NSString        *forNavn;
    NSString        *mellemNavn;
    NSString        *efterNavn;
    NSString        *gender;

}

@property (nonatomic, retain)   NSString    *forNavn;
@property (nonatomic, retain)   NSString        *mellemNavn;
@property (nonatomic, retain)   NSString        *efterNavn;
@property (nonatomic, retain)   NSString        *gender;
@property (nonatomic, strong) NSMutableArray    *arrayA;

UPDATE 6:

I figured out that i can pass the data to DetailviewContainerController with this

    DetailViewContainerController *test = (DetailViewContainerController *)    [[self.splitViewController.viewControllers lastObject] topViewController];
    test.leftViewController = self;
    test.forNavn = forNavn;
    NSLog(@"test = %@",test.forNavn);

but now i can't get it further from DetailViewContainerController. i tried this :

VelkommenViewController *test2 = (VelkommenViewController *)    [[self.splitViewController.viewControllers lastObject] topViewController];
        test2.detailViewContainerController = self;
        test2.forNavn = self.forNavn;
        NSLog(@"test2 = %@",test.forNavn);

Update 7:

enter image description here

Update 8:

 VelkommenViewController *test2 = [[[self.splitViewController.viewControllers.lastObject topViewController] childViewControllers] objectAtIndex:0];
test2.forNavn = forNavn;
NSLog(@"test3 = %@",test2.forNavn);

Throws a error : -[UINavigationController setForNavn:]: unrecognized selector sent to instance 0x175b56c0

Update 9:

new storyboard

Was it helpful?

Solution 2

on the receiving end this works:

    NSString *name = ((DetailViewContainerController *)self.parentViewController).forNavn;

OTHER TIPS

It's a little unclear what you're doing, especially with the child view controller stuff. But, in general, when working with a split view controller, the detail controller can be accessed by using self.splitViewController.viewControllers[1] (if the detail controller is embedded in a navigation controller, then that gets you a reference to the navigation controller, and you have to use its topViewController property to get to the detail controller). You shouldn't be using instantiateViewControllerWithIdentifier:, or alloc init to access the detail controller, since either of those methods will give you a new instance instead of referencing the one you have on screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top