【发布时间】:2013-11-19 02:31:39
【问题描述】:
我今天在想这个,现在我测试了我有点困惑......
当通过将 viewController 推送到导航堆栈或以模态方式呈现 ViewController 来使用 viewControllers 时,我想知道内存管理。
让我们使用模态示例作为思想实验,这里是创建和呈现视图的源代码,在我的示例中,ARC 与否无关紧要,因此两者兼而有之:
使用 ARC:
ViewController *myViewController = [[ViewController alloc] init];
myViewController.delegate = self;
[self presentViewController:myViewController animated:YES completion:NULL];
没有 ARC:
ViewController *myViewController = [[ViewController alloc] init];
myViewController.delegate = self;
[self presentViewController:myViewController animated:YES completion:NULL];
[myViewController release]; //As it's now 'owned' by the presenting View controller
这将是我对如何在现有 ViewController 上以模态方式呈现 viewController 的理解。
对于我们的示例,上面的代码驻留在一个方法中,当触摸按钮以呈现 ViewController 时调用该方法。
现在我的问题,
我正在做的是每次触摸按钮时调用此代码,在使用 Instruments 进行测试期间,我似乎没有任何泄漏。 - 然而 因为我在 myViewController dealloc 和 viewDidLoad 方法中有 NSLog 语句,所以我知道每次触摸按钮时它都会被实例化,但从未解除分配。
所以……
A) 为什么我没有在仪器中看到泄漏(或 Live Bytes 增加)(使用 ARC 或不是)因为我似乎在创建一个新的 viewController 并在每次展示它时泄漏旧的。
B) 如果这不是内存安全的,那么编写上述代码的正确方法是什么?我在 Apple 的示例代码和互联网上都看到了这种代码 sn-ps。我(和他们)是否应该将 alloc init 行包装在 if 语句中以检查对象是否已创建?
即
if(!myViewController)
{
ViewController *myViewController = [[ViewController alloc] init];
}
myViewController.delegate = self;
[self presentViewController:myViewController animated:YES completion:NULL];
感谢您抽出宝贵时间阅读和回答,我真的很想知道这一点,因为我一直在使用上面的代码创建、推送和呈现 ViewController,并且从未注意到泄漏! - 可能不得不回去重写它!
为避免混淆,请注意:delegate 属性是我的 UIViewController 子类(我在其中实现了委托协议)的自定义属性,需要正确关闭 Modally 呈现的 Viewcontroller。根据编码指南。
问候, 约翰
按要求编辑,创建委托:
.h
@protocol NotificationManagementViewControllerDelegate;
@interface NotificationManagementController :
{
__weak NSObject <NotificationManagementViewControllerDelegate> *delegate;
}
@property (nonatomic, weak) NSObject <NotificationManagementViewControllerDelegate> *delegate;
@protocol NotificationManagementViewControllerDelegate <NSObject>
@optional
- (void)didFinishSettingNotification:(NotificationManagementController *)notificationManagementController;
.m
- (void)sendMessageToDismiss {
if ([[self delegate] respondsToSelector:@selector(didFinishSettingNotification:)]) {
[self.delegate didFinishSettingNotification:self];
}
}
最后是代表.m:
- (void)didFinishSettingNotification:(NotificationManagementController *)notificationManagementController
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
【问题讨论】:
标签: ios iphone objective-c uiviewcontroller uikit