dismissModalViewControllerAnimated不释放视图控制器

时间:2012-02-22 16:38:56

标签: ios

作为我的app启动的一部分,我循环浏览了几个视图控制器。一切正常,但视图控制器未发布。 (从不调用viewDidUnload和dealloc)。在每次状态更改之前,我会关闭任何呈现的视图控制器。它们被解散,并为每个视图控制器调用viewWillDisappear:

- (void)stateChange:(NSString *)state {

// Dismiss any exiting modals/popups
[self dismissPopup];
[self dismissModalViewControllerAnimated: NO];        

if([state isEqualToString:@"StateBoot"]) {

    CLRemotePrimaryBootViewController * viewControllerBoot = [[CLRemotePrimaryBootViewController alloc]initWithNibName: @"CLRemoteBootView" bundle:nil];
    [self presentModalViewController:viewControllerBoot animated:NO];   
    [viewControllerBoot release];
}

else if([state isEqualToString:@"StateLogin"]) {

    CLRemotePrimaryAuthViewController *viewControllerAuth = [[CLRemotePrimaryAuthViewController alloc]initWithNibName: @"CLRemoteAuthView" bundle:nil];
    [self presentModalViewController:viewControllerAuth animated:NO]; 
    [viewControllerAuth release];
}

else if([state isEqualToString:@"StateMain"]) {

    [self setSelectedIndex:0];
}

else if([state isEqualToString:@"StateStop"]) {

    // TBD
}

}

解雇后,self.presentedViewController实际上是零。关于如何强制iOS发布这些未使用的视图控制器的任何想法?

编辑 -

解决了!希望这篇文章能帮助别人 - 我感到很困惑。事实证明,我的模态视图控制器是从在init方法中设置观察者(NSNotificationCenter)的基类派生的。必须先删除这些观察者,然后才能释放观察视图控制器。在我的情况下,这变得更加困难,因为观察者是块而不是选择器。添加这些观察者时,会为每个观察者返回id - 所以我必须在NSMutableArray中跟踪这些并添加一个方法来释放我的基类中的观察者:

+ (void) safeUnsubscribe:(id)object {

if ( object != nil ) {

    if ( [object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) {

        CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object;
        [viewController unsubscribe];
    }        
}

} `

` - (void)handle:(NSString *)name usingBlock:(CLObjBlock)block {

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:^(NSNotification * notification){ block([notification object]); }];
[self.observers addObject:observer];    

}`

// Must call before dealloc or listener will never be released!

` - (void)取消订阅{

for ( id observer in self.observers ) {

    [[NSNotificationCenter defaultCenter] removeObserver:observer];
}

[self.observers removeAllObjects];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

2 个答案:

答案 0 :(得分:1)

解决了!希望这篇文章能帮助别人 - 我感到很困惑。事实证明,我的模态视图控制器是从在init方法中设置观察者(NSNotificationCenter)的基类派生的。必须先删除这些观察者,然后才能释放观察视图控制器。在我的情况下,这变得更加困难,因为观察者是块而不是选择器。添加这些观察者时,会为每个观察者返回id - 所以我必须在NSMutableArray中跟踪这些并添加一个方法来释放我的基类中的观察者:

+ (void) safeUnsubscribe:(id)object {

if ( object != nil ) {
    if ( [object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) {

        CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object;
        [viewController unsubscribe];
    }        
}

} 



- (void)handle:(NSString *)name usingBlock:(CLObjBlock)block {

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:^(NSNotification * notification){ block([notification object]); }];
[self.observers addObject:observer];    

}


// Must call before dealloc or listener will never be released!

- (void)unsubscribe {

for ( id observer in self.observers ) {

    [[NSNotificationCenter defaultCenter] removeObserver:observer];
}

[self.observers removeAllObjects];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

答案 1 :(得分:0)

视图控制器内部是尚未发布的对象。为每个视图控制器定义dealloc方法(记得调用[super dealloc])并设置断点以确定是否正在调用它。

在每个视图控制器中查找已分配的任何对象并确保它已被释放。