我花了一整天的时间来调试为什么我的UIViewController
在被解雇后没有被解除分配,我把它缩小到我设置一个rightBarButtonItem的事实。我的项目正在使用ARC。
在我的appDelegate
中,如果用户尚未登录,我会提供“欢迎”屏幕:
// AppDelegate.m
WelcomeViewController *welcome = [[WelcomeViewController alloc] initWithContext:AuthenticationPresentationContextSplash];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:welcome];
[self.window.rootViewController presentViewController:nav animated:false completion:nil];
然后,当用户登录或点击rightBarButtonItem时,我会关闭viewController:
// WelcomeViewController.m
- (IBAction)onSkipOrCancel
{
[self.presentingViewController dismissViewControllerAnimated:true completion:nil];
}
我已经确认在AppDelegate
和WelcomeViewController
中,self.window.rootViewController和self.presentingViewController引用相同UIViewController的完全相同的实例。
奇怪的是,经过一整天的工作,我发现问题出现在这段代码中:
// also in WelcomeViewController.m
- (void)configureRightBarButtonItem
{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"Skip"
style:UIBarButtonItemStylePlain
target:self
action:@selector(onSkipOrCancel)];
// if i comment this out, when onSkipOrCancel runs and the presentingViewController
// is sent dismissViewController, WelcomeViewController will successfully get deallocated
self.navigationItem.rightBarButtonItem = rightItem;
}
似乎在设置rightBarButtonItem
时创建了对WelcomeViewController的强引用。为什么会这样,我该如何解决呢?
答案 0 :(得分:0)
试试这个
// also in WelcomeViewController.m
- (void)configureRightBarButtonItem
{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"Skip"
style:UIBarButtonItemStylePlain
target:self
action:@selector(onSkipOrCancel)];
// if i comment this out, when onSkipOrCancel runs and the presentingViewController
// is sent dismissViewController, WelcomeViewController will successfully get deallocated
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];//whenever we alloc we should release it too!
}
答案 1 :(得分:0)
在一天加剧之后,事实证明这是Taplytics v1.2.24中的一个错误。该错误已在1.2.32中修复。
他们在UIBarButtonItem上有一个类别,他们在那里进行一些方法调整,并且持有对rightBarButtonItem(这是我的viewController)的目标的强引用。这反过来阻止我的UIViewController解除分配。