سؤال

I am instantiating a viewcontroller using instantiateViewControllerWithIdentifier. Do I have to expect it to be completely released when I assigned nil to VC variable in ARC setup or storyboard would retain it anyway?

I am presenting viewcontroller modally using presentViewController, then inside vc dismissing it. However, I don't see dealloc method of vc is being called.

Update:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:NULL];
RViewControllerEx *viewController = (RViewControllerEx *)[sb instantiateViewControllerWithIdentifier:@"SB_RDVC"];
[self presentViewController:viewController animated:YES completion:nil];
  • I have NSLog in -dealloc method to determine it's called or not
  • I'm using local variable and it's not deallocating immediately
هل كانت مفيدة؟

المحلول

You haven't provided enough information. Are you using ARC? Are you using a local variable to hold a pointer to the VC, or an instance variable?

How are you concluding that the modal VC's dealloc isn't being called?

If you instantiate your VC using a strong local variable and then call presentViewcontroller:animated:completion:, the presenting view controller will keep a strong reference to your VC while it is being displayed. (You need to use a strong local variable or the newly created VC will be deallocated as soon as you create it.

As soon as the modal is done being dismissed, the presenting view controller will clear it's strong reference, and the modal should be deallocated.

نصائح أخرى

Dealloc is not called right away. With ARC the system will automatically clean up views that are no longer being used when it needs to.

In your case, once the view is dismissed, it will hang around until the system needs the space.

Depending on what you are attempting/ need to do in the dealloc there are a few solutions (post your needs if interested)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top