我有__弱自我引用。控制器2和控制器3是否需要参考竞争区块中的__weak参考?__自我阻止
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
id controller1 = [sb instantiateViewControllerWithIdentifier:@"controller1"];
id controller2 = [sb instantiateViewControllerWithIdentifier:@"controller2"];
id controller3 = [sb instantiateViewControllerWithIdentifier:@"controller3"];
__weak typeof(self) weakSelf = self;
[self presentViewController:controller1 animated:YES completion:^{
[(UINavigationController *)weakSelf.parentViewController setViewControllers:@[controller2, controller3] animated:NO];
}];
编辑什么下面的代码?以下代码中的块是否需要对自我的弱引用?
typedef void(^MyCustomBlock)(void);
@property (strong, readwrite, nonatomic) MyCustomBlock customBlock;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
__weak typeof(self) weakSelf = self;
self.customBlock = ^{
[(UINavigationController *)weakSelf.parentViewController setViewControllers:@[controller2, controller3] animated:NO];
};
[self presentViewController:controller1 animated:YES completion:self.customBlock];
}
你想做什么?这将提供一个视图控制器,然后立即更改导航控制器显示的视图控制器(您应该通过'self.navigationController',BTW) – jcaron
忘记应用程序逻辑。我是否需要添加行:__weak typeof( controller1)weakController1 = controller1; __weak typeof(controller2)weakController2 = controller2? – Adobels
如果你需要它们,编译器会告诉你。通常只有当你引用自我或实例变量(隐含引用self)时才需要它们。 – jcaron