iPhone - 使用类属性时释放崩溃

发布于 2024-10-23 01:05:20 字数 529 浏览 4 评论 0原文

简单的问题...

为什么当我单击overlayController 视图上的按钮时,这个release 会使我的应用程序崩溃?

    OverlayViewController* overlayController = [[OverlayViewController alloc]
                                                initWithNibName:@"Overlay"
                                                         bundle:nil];
    self.picker.cameraOverlayView = overlayController.view;
    [overlayController release];

换句话说,如何在 viewDidLoad 方法之外调用 OverlayViewController 方法?

奇怪...我不明白为什么我的应用程序可以工作:-)

Simple question...

Why does this release crash my app when I click on a button that is on the overlayController view?

    OverlayViewController* overlayController = [[OverlayViewController alloc]
                                                initWithNibName:@"Overlay"
                                                         bundle:nil];
    self.picker.cameraOverlayView = overlayController.view;
    [overlayController release];

Said another way, how can the OverlayViewController methods be called outside of this viewDidLoad method?

Strange... I do not understand why my app is working :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-10-30 01:05:20

好吧,我可以很容易地回答第二个问题:

如何在此 viewDidLoad 方法之外调用 OverlayViewController 方法?

只需将一个属性添加到您的 viewController 中:

MyViewController.h:

@interface MyViewController : UIViewController {
    OverlayViewController* overlayController;
}
@property (nonatomic, retain) OverlayViewController* overlayController;
@end

MyViewController.m:

@implementation MyViewController
@synthesize overlayController;

- (void)viewDidLoad {
    OverlayViewController* ovc = [[OverlayViewController alloc]
                                   initWithNibName:@"Overlay" bundle:nil];
    self.overlayController = ovc;
    [ovc release];
    self.picker.cameraOverlayView = self.overlayController.view;
}

- (void)someOtherMethod {
    [self.overlayController someMethod];
}

- (void)dealloc {
    [overlayController release];
    [super dealloc];
}

@end

Well, I can answer the second question easily enough:

How can the OverlayViewController methods be called outside of this viewDidLoad method?

Simply add a property to your viewController:

MyViewController.h:

@interface MyViewController : UIViewController {
    OverlayViewController* overlayController;
}
@property (nonatomic, retain) OverlayViewController* overlayController;
@end

MyViewController.m:

@implementation MyViewController
@synthesize overlayController;

- (void)viewDidLoad {
    OverlayViewController* ovc = [[OverlayViewController alloc]
                                   initWithNibName:@"Overlay" bundle:nil];
    self.overlayController = ovc;
    [ovc release];
    self.picker.cameraOverlayView = self.overlayController.view;
}

- (void)someOtherMethod {
    [self.overlayController someMethod];
}

- (void)dealloc {
    [overlayController release];
    [super dealloc];
}

@end
初相遇 2024-10-30 01:05:20

set方法不保留视图吗
属性

是的,但它不保留 overlayController ,它会在 [overlayController release] 后愉快地释放。

为了能够调用overlayController方法,请将其保存在实例变量中。无论如何你都必须这样做以避免崩溃和/或泄漏。

Doesn't the set method retain the view
attribute

Yes but it doesn't retain overlayController which will be happily deallocated after [overlayController release].

To be able to call overlayController methods keep it in instance variable. You have to do this anyway to avoid crashes and/or leaks.

定格我的天空 2024-10-30 01:05:20

您将overlayController 的视图分配给您的属性,但随后释放整个控制器。 时,setter 才会保留。

@property(retain)NSArray * myArray;

仅当您指定了此设置(如查看cameraOverlayView 的属性,如果这确实保留了视图)

You're assigning the view of the overlayController to your property but release the whole controller afterwards. The setter does only retain if you've specified this like

@property(retain)NSArray * myArray;

Have a look at the property of cameraOverlayView if this really retains the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文