如何制作pushViewController:在以下情况下工作?

时间:2013-02-11 13:28:13

标签: ios uinavigationcontroller pushviewcontroller

我在RootViewController中嵌入了导航控制器。 RootViewController中存在的元素是分段控件和UIView。在选择不同的段时,UIView会显示相应的ViewController。

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

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

我可以根据段更改显示UITableViewControllers。但是,当我点击表格的行时,即使我在故事板中创建了特定的ViewController并设置了标识符,所以出现的较新的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. 创建一个名为newClass的类,其中包含我想要传递的所有元素。

  2. 在firstViewController中创建一个NSMutableArray“newObjects”。

  3. 在此方法中创建每个newClass对象。

    • (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; } 请原谅我...因为代码块的缩进搞砸了...... 此外,PFObject是因为我使用Parse作为数据库。

  4. 将创建的对象添加到newObjects数组。

  5. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中,这是代码

    • (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(@“选择的行是%ld”,(长)path.row); //这会返回正确的选定行

      NSLog(@“选中的电影是%@”,[[newObjects objectAtIndex:path.row] objName]); //然而,根据cellForRowAtIndexPath方法,这不是正确的。

      [newController setNewObj:obj];

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

  6. 运行此代码时,正确选择了行。但是我没有得到数据库中的相应行。该阵列存储有冗余数据。

1 个答案:

答案 0 :(得分:1)

你需要摆脱那条中间线:

newController = [[newDetailController alloc] init];

这只是重新定义了newController(作为没有设置视图的空控制器),您在第一行中定义了它。