如何使用UISplitViewController中的didSelectRow传递数据 - 从主数据到详细信息

时间:2014-03-29 11:32:05

标签: ios objective-c uitableview uisplitviewcontroller

我想将一些核心数据从master传递给detail。

首先,用户选择已分配性别的名称。如果性别女孩" pige"它去了女孩UITableiew。从那里每个选择显示详细视图。 我想从女孩UITableiew传递一些数据。就像它的女孩颜色一样粉红色。设置标题等 到目前为止,我知道如何传递UITableiew" master"但不是如何将它带到详细视图

这里有一些关于如何管理UITableView等选择的代码。

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];

}

在女孩menu.h中,我有想要通过的数据的NSStrings。这样才有用。但是怎么把它送到detailviewcontroller

更新 我试过这个Accessing array from a detail view controller into master view controller in Objective-C

但无法正常工作

更新2:

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

更新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]];
}

更新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;

更新6:

我发现我可以使用此

将数据传递给DetailviewContainerController
    DetailViewContainerController *test = (DetailViewContainerController *)    [[self.splitViewController.viewControllers lastObject] topViewController];
    test.leftViewController = self;
    test.forNavn = forNavn;
    NSLog(@"test = %@",test.forNavn);

但现在我无法从DetailViewContainerController进一步了解。我试过这个:

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

更新7:

enter image description here

更新8:

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

引发错误: - [UINavigationController setForNavn:]:无法识别的选择器发送到实例0x175b56c0

更新9:

new storyboard

2 个答案:

答案 0 :(得分:0)

有点不清楚你在做什么,特别是对于儿童视图控制器的东西。但是,一般情况下,使用拆分视图控制器时,可以使用self.splitViewController.viewControllers [1]访问详细控制器(如果详细控制器嵌入在导航控制器中,那么可以获得对导航的引用控制器,你必须使用它的topViewController属性来获取细节控制器)。您不应该使用instantiateViewControllerWithIdentifier:或alloc init来访问详细控制器,因为这些方法中的任何一个都会为您提供一个新实例,而不是引用您在屏幕上显示的实例。

答案 1 :(得分:0)

接收端的

有效:

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