- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定以下代码示例(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/
我正在使用 tcod-rs。用于绘制到 RootConsole 的每个方法都采用一个可变引用。中央循环是一个 while 循环,它等待窗口关闭、清除屏幕、绘制,然后刷新。 “检查窗口关闭”方法也采用可
我写了一个具有这种形式的函数: 结果 f(const IParameter& p); 我的意图是这个签名将明确表明函数没有取得参数 p 的所有权。 问题是 Result 将保留对 IParameter
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: What is a smart pointer and when should I use one? 我正在阅
假设我有一个类: class Scheduler { Scheduler(JobService *service); AddJob(JobID id, ISchedule *sched
我试图弄清楚所有权如何与函数 CVMetalTextureGetTexture 一起工作: CVMetalTextureRef textureRef; // ... textureRef is cre
这个问题在这里已经有了答案: Should we pass a shared_ptr by reference or by value? (10 个答案) 关闭 4 年前。 例如 class A {
我正在做一个附带项目,我需要根据他的 gmail 帐户或任何其他参数来验证 channel 是否属于某个用户……这基本上是为了避免假帐户。是否可以? 最佳答案 是的, 跟随 youtube 记录的链接
我在使用Core Foundation Array时发现了一个奇怪的问题!这是代码片段 fname = CFStringCreateWithFormat(kCFAllocatorDefault, NU
有没有一种方法可以设置在 apache 下运行的 php 来创建文件夹,该文件夹的文件夹属于创建它的程序的所有者,而不是由 apache 拥有? 使用 word press 它会创建要上传到的新文件夹
我编写了以下函数来使用 boost.date_time 获取日期/时间字符串. namespace bpt = boost::posix_time; string get_date_time_stri
我在使用 Docker 容器时遇到了一个有点烦人的问题(我在 Ubuntu 上,所以没有像 VMWare 或 b2d 这样的虚拟化)。我已经构建了我的镜像,并且有一个正在运行的容器,它有一个来 sel
根据大多数示例,逻辑上最少有 3 个组织 ( org1, org2, orderer )。 实际上只有 2 个物理组织 ( org1, org2 )。任一组织或约定的第 3 方必须移交订购者组织的职责
我开始学习 Rust,在进行实验时,我发现所有权如何应用于我不理解的元组和数组的方式有所不同。基本上,以下代码显示了差异: #![allow(unused_variables)] struct Inn
我们有一个应用程序,其表单上有许多组件(面板、选项卡、编辑、组合框等)。但根据用户配置文件,其中大多数可以自动填充和/或不可见。因此,用户可以更快地完成工作。 问题:是否有更简单的方法可以在运行时创建
我有以下代码片段: fn f u32>(c: T) { println!("Hello {}", c()); } fn main() { let mut x = 32; let
我想执行示例中的代码: require_once 'google-api-php-client/vendor/autoload.php'; $client = new Google_C
这个问题在这里已经有了答案: What is move semantics? (11 个答案) 关闭 3 年前。 我有一个看起来像这样的构造函数: Thing::Thing(std::vector
我们正在使用服务帐户从服务器上传文件,但它已达到其存储配额限制。所有文件都已添加到另一个用户(具有 100 Gb 存储配额的 @gmail.com 帐户)创建的文件夹下,但上传的所有文件均归该服务帐户
我正处于 this question 中描述的 sme 情况。 .那个提问者找到的解决方案是 Full access !== Owner. I need to read the documentati
我正处于 this question 中描述的 sme 情况。 .那个提问者找到的解决方案是 Full access !== Owner. I need to read the documentati
我是一名优秀的程序员,十分优秀!