正在引用的iOS复制/僵尸视图控制器

时间:2012-03-27 21:05:41

标签: objective-c ios ios5 uikit

我有一个由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];我无法弄清楚为什么它会引用我传入initWithRootViewController:mainView的那个。

以下是两个MainViewControllers。我初始化的第一个,第二个是在makeKeyAndVisible中创建的。

enter image description here

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

enter image description here

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

根据要求:

- (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;
}

1 个答案:

答案 0 :(得分:1)

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