UINavigationItem TitleView在推送新视图时崩溃

时间:2012-08-14 19:42:37

标签: objective-c uinavigationbar uinavigationitem

在视图中加载了视图控制器,我使用图像自定义标题视图:

self.navigationItem.titleView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"navigationBarCentralImage.png"]];

到目前为止,它在导航栏的中心显示图像。 但是,当我弹出当前视图并推送一个运行良好而没有标题视图自定义的新视图时,它会崩溃。 这是我用来弹出/推送

的代码
-(void)popLastViewAndPush:(id)viewController
{
    NSMutableArray *controllers = [SharedAppDelegate.navigationController.viewControllers mutableCopy];
    if([controllers objectAtIndex:[controllers count] -1] == viewController)
        return;

    [controllers removeLastObject];
    SharedAppDelegate.navigationController.viewControllers = controllers;

    [SharedAppDelegate.navigationController pushViewController:viewController animated:YES];
}

我有点陷入困境,不明白为什么它实际上会与标题视图自定义崩溃

1 个答案:

答案 0 :(得分:0)

如果您不使用ARC,请将代码行更改为:

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage: [UIImage imageNamed: @"navigationBarCentralImage.png"]] autorelease];

必须向分配的对象添加autorelease