0

我有一个嵌入在 RootViewController 中的导航控制器。RootViewController 中存在的元素是分段控件和 UIView。As the different segments are selected, the UIView displays a corresponding ViewController.

现在在 UIView 中显示的每个 ViewController 都是一个 UITableViewController。

我需要根据段选择显示表值。而且,如果选择了表格的任何行,它必须移动到具有详细视图的更新的 ViewController。

我能够根据段更改显示 UITableViewControllers。但是,当我单击表格的行时,出现的较新的 ViewController 是空白的(黑屏),即使我已经在 Storyboard 中创建了特定的 ViewController 并设置了一个标识符。

ViewController.m 中分段控制更改的代码

- (void)segmentedControlChangedValue:(UISegmentedControl*)segmentedControl {

int selIndex = segmentedControl.selectedIndex;

if (selIndex == 0)
{
    aTable = [[aTableViewController alloc] init];

    aTable.view.frame = self.myView.bounds;
    [self.myView addSubview:aTable.view];
    [self addChildViewController:aTable];
}

if (selIndex == 1)
{
    bTable = [[bTableViewController alloc] init];

    bTable.view.frame = self.myView.bounds;
    [self.myView addSubview:bTable.view];
    [self addChildViewController:bTable];

}

if (selIndex == 2)
{
    //NSLog (@"Well you selected a 3rd option");
}}

didSelectRowAtIndexPath 的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];

newDetailController* newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];

newController = [[newDetailController alloc] init];

[[self navigationController] pushViewController:newController animated:YES];}

即使我已经指定了要在 Storyboard 设计中使用的 ViewController 并且还使用了标识符,pushViewController 也会为我推送一个空视图。

请帮助我理解我必须做些什么来纠正这个错误。

更新 - 上述问题已解决。

我现在面临的问题是无法访问 newViewController 中的数据。

我将数据传递给 newViewController,如下所示:

  1. Create a class named newClass which contains all the elements I want to pass across.

  2. Create a NSMutableArray "newObjects" in the firstViewController.

  3. Create each newClass object at this method.

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString *CellIdentifier = @"Cell";

    customBigTableCell *cell = (customBigTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) { cell = [[customBigTableCell alloc] init]; }

    cell.name.text = [object objectForKey:@"objName"];

    newClass* newObj = [[newClass alloc] init];

    newObj.objName = cell.name.text;

    [newObjects addObject:newObj];

    return cell; } Pls pardon me .. for the indenting with the code block is messed up... Also the PFObject is cos i use Parse for database.

  4. Add the created object to the newObjects array.

  5. In the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, this is the code

    • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [super tableView:tableView didSelectRowAtIndexPath:indexPath];

      UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

      newDetailController* newController = [storyboard instantiateViewControllerWithIdentifier:@"UpcomingDetail"];

      obj = [[newClass alloc] init];

      NSIndexPath* path = [self.tableView indexPathForSelectedRow];

      obj = [newObjects objectAtIndex:path.row];

      NSLog(@"Row selected is %ld", (long)path.row); //this returns the correct row selected

      NSLog(@"Movie selected is %@", [[newObjects objectAtIndex:path.row] objName]);//however this is not the right one as per the cellForRowAtIndexPath method.

      [newController setNewObj:obj];

      [[self navigationController] pushViewController:newController animated:YES]; }

When I run this code, the row is selected correctly. However I dont get the corresponding row in the database. The array gets stored with redundant data.

4

1 回答 1

1

You need to get rid of that middle line:

newController = [[newDetailController alloc] init];

That just redefines newController (as an empty controller with no view set up), which you defined in the first line.

于 2013-02-11T16:29:53.353 回答