我有__弱自我引用。控制器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