gpt4 book ai didi

ios - dismissModalViewControllerAnimated 不释放 View Controller

转载 作者:可可西里 更新时间:2023-11-01 04:54:33 24 4
gpt4 key购买 nike

作为我的应用程序启动的一部分,我循环使用多个 View Controller 。一切正常,但 View Controller 未发布。 (永远不会调用 viewDidUnload 和 dealloc )。在每次状态更改之前,我都会关闭所有呈现的 View Controller 。它们被解雇,并为每个 View Controller 调用 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 实际上为 nil。关于如何强制 iOS 释放这些未使用的 View Controller 有什么想法吗?

编辑 -

解决了!希望这篇文章对其他人有所帮助 - 我感到困惑。事实证明,我的模态视图 Controller 派生自一个在 init 方法中设置观察者 (NSNotificationCenter) 的基类。在释放观察 View Controller 之前,必须移除这些观察者。在我的例子中,这变得更加困难,因为观察者是 block 而不是选择器。添加这些观察者时,每个观察者都会返回 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];

}`

//必须在 dealloc 之前调用,否则 listener 永远不会被释放!

`-(无效)取消订阅{

for ( id observer in self.observers ) {

[[NSNotificationCenter defaultCenter] removeObserver:observer];
}

[self.observers removeAllObjects];

[[NSNotificationCenter defaultCenter] removeObserver:self];

最佳答案

解决了!希望这篇文章对其他人有所帮助 - 我感到困惑。事实证明,我的模态视图 Controller 派生自一个在 init 方法中设置观察者 (NSNotificationCenter) 的基类。在释放观察 View Controller 之前,必须移除这些观察者。在我的例子中,这变得更加困难,因为观察者是 block 而不是选择器。添加这些观察者时,每个观察者都会返回 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];

}

关于ios - dismissModalViewControllerAnimated 不释放 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9399030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com