【问题标题】:iOS duplicate/zombie view controller being referencediOS 重复/僵尸视图控制器被引用
【发布时间】:2012-03-27 21:05:41
【问题描述】:

我有一个由 nib 构建的 MainWindow 类,设置如下:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainViewController"
                                                                        bundle:nil];
    if(!mainView)
    {
        return;
    }
    naviController = [[UINavigationController alloc] initWithRootViewController:mainView];
    [naviController setToolbarHidden:YES];
    [[naviController navigationBar] setTintColor:[UIColor blackColor]];
    [[naviController toolbar] setTintColor:[UIColor blackColor]];
    [self.window setRootViewController:naviController];
    [self.window makeKeyAndVisible];
}

这可以正常工作并正确显示MainViewController,但是当我尝试在MainViewController 的表格视图中向下滚动时,它会抛出EXC_BAD_ACCESS。显然 UIKit 指的是第二个 MainViewController,它是在 [self.window makeKeyAndVisible]; 中构建的

这是两个MainViewControllers。第一个我初始化,第二个在makeKeyAndVisible创建。

这是第二个被称为僵尸的MainViewController。

关于为什么会发生这种情况的任何想法?

根据要求:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainViewCell"];
    int i = [indexPath row];
    NSLog(@"%d\n",i);
    if (cell == nil) {
        // Create a temporary UIViewController to instantiate the custom cell.
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"MainViewCell" bundle:nil];
        // Grab a pointer to the custom cell.
        cell = (MainViewCell *)temporaryController.view;
        // Release the temporary UIViewController.
        [temporaryController release];
    }
    [[cell icon] setImage:[UIImage imageNamed:[[moduleXMLList objectAtIndex:i] objectForKey:@"thumbnail"]]];
    [[cell title] setText:[[moduleXMLList objectAtIndex:i] objectForKey:@"title"]];
    [[cell description] setText:[[moduleXMLList objectAtIndex:i] objectForKey:@"description"]];
    return cell;
}

【问题讨论】:

  • 你试过清理所有目标吗?
  • 现在我有了。结果是一样的。
  • 除此之外,解决此类问题的方法通常是重置模拟器中的内容和设置,重启 Xcode,必要时重启计算机
  • 哇,这看起来很奇怪。它可能类似于这个问题 - stackoverflow.com/questions/3739405/… 吗?烦人地没有合适的解决方案,但也许有帮助?
  • 还是没有运气。可能还值得一提的是,我正在加载自定义 UITableViewCells,如 bdunagan.com/2009/06/28/… 所述,虽然最初加载很少,但当我尝试向下滚动到新单元格时会引发异常。话虽如此,它不是从我的任何方法中抛出的。

标签: objective-c ios ios5 uikit


【解决方案1】:

问题是我使用 File's Owner 作为 MainViewController,而在 Objects 下有第二个 UIViewController。这是错误的,所以一旦我摆脱了对象并只使用文件的所有者,它就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多