gpt4 book ai didi

ios - 为什么 UINavigationController 无法使用 ARC 获取 block 回调参数的所有权

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:15 26 4
gpt4 key购买 nike

给定以下代码示例(iOS 7、Xcode 5):

/**
* SampleProvider Class
*/

typedef void(^RequestCallback)(UIViewController *result);

static NSString * const cControllerRequestNotification = @"controllerRequestNotification";
static NSString * const cRequestClassNameKey = @"className";
static NSString * const cRequestCallbackKey = @"callback";

@interface SampleProvider : NSObject
+ (void)requestControllerForClassName:(NSString *)className completion:(RequestCallback)callback;
@end

@interface SampleProvider ()
- (UIViewController *)controllerForClassName:(NSString *)className;
- (void)didReceiveControllerRequest:(NSNotification *)n;
@end

@implementation SampleProvider

#pragma mark - Overrides
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)init {
self = [super init];
if( self ) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveControllerRequest:) name:cControllerRequestNotification object:nil];
}
return self;
}

#pragma mark - Public API

+ (void)requestControllerForClassName:(NSString *)className completion:(RequestCallback)callback{

NSDictionary *requestInfo = @{ cRequestClassNameKey : className, cRequestCallbackKey : [callback copy] };
[[NSNotificationCenter defaultCenter] postNotificationName:cControllerRequestNotification object:requestInfo];
}

#pragma mark - Private API

- (UIViewController *)controllerForClassName:(NSString *)className {
UIViewController *result = nil;
Class controllerClass = NSClassFromString(className);
if( (nil != controllerClass) && ([controllerClass isSubclassOfClass:[UIViewController class]]) ) {
result = [[controllerClass alloc] init];
}
return result;
}

- (void)didReceiveControllerRequest:(NSNotification *)n {
NSDictionary *requestInfo = [n object];
NSString *className = requestInfo[cRequestClassNameKey];
RequestCallback callback = requestInfo[cRequestCallbackKey];

UIViewController *result = [self controllerForClassName:className];

if( nil != callback ) {
callback(result);
}
}

@end

/**
* SampleViewController Class
*/

@interface SampleViewController : UIViewController
@end
@implementation SampleViewController

#pragma mark - Overrides
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

NSString *className = @"ClassName";

[SampleProvider requestControllerForClassName:className completion:^(UIViewController *result) {
if( nil != result ) {
// Result is valid pointer, not a zombie.
[self.navigationController pushViewController:result animated:YES];
// Result is released, not nil.
} else {
NSLog(@"Unable to load controller with class name: %@", className);
}
}];
}
@end

为什么我的 UINavigationController 无法获得由 SampleProvider 的公共(public)类方法接收的回调 Controller 的所有权,即使在显示 View 之后也是如此?

我看到了以下行为:

  • 新 Controller 类已正确分配并通过回调方法返回。进入回调后,结果参数指向有效内存。

  • 新 Controller 被推送到我的 UINavigationController 的导航堆栈。

  • 调用新推送的 Controller 的“viewDidLoad”方法。

  • 检查 UINavigationController 的“viewControllers”属性时,数组中引用了新推送的 Controller 。

  • 新的推送 Controller 被释放,而 UINavigationController pushViewController:animated: 仍在执行。

  • 新 Controller 现在是僵尸。

感谢您的帮助。

最佳答案

我没有明确的答案,因为答案可能在您尚未发布的代码中——除了两个观察结果(可能会引导您找到答案)之外,您发布的代码看起来有效:

  • isKindOfClass 应该是 isSubclassOfClass 吗? -isKindOfClass:是一个NSObject 上的实例方法,而不是类方法。

  • 似乎在 viewDidLoad 期间同步调用 pushViewController:危险的。 View 层次结构的状态很有可能那个时候不稳定。这种插入应该发生在响应我想是其他一些离散事件。尝试插入(或整个 requestControllerForClassName:) 异步通过dispatch_async,作为测试,看看是否能解决您的问题。

关于ios - 为什么 UINavigationController 无法使用 ARC 获取 block 回调参数的所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316323/

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