么在viewDidUnload中将nil分配给IBOutlets

么在viewDidUnload中将nil分配给IBOutlets

本文介绍了为什么在viewDidUnload中将nil分配给IBOutlets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController ,其中有一个 UILabel IBOutlet

I have a UIViewController that has an IBOutlet for a UILabel with the label wired up in the XIB.

#import <Foundation/Foundation.h>

@interface MyViewController : UIViewController {
    IBOutlet UILabel *aLabel; 
}
@end

根据第7章

因此,建议在 viewDidUnload 中释放标签。

Thus it recommends releasing the label in viewDidUnload.

- (void)viewDidUnload {
   [super viewDidUnload];
   [aLabel release];
   aLabel = nil;
}

作为C#程序员,我已经把分配nil的想法鼓了起来/无意义是没有意义的。虽然我可以在Objective-C中看到它更有意义,但仍然使我的代码美感略有提高*。我将其删除,并且一切正常。

Being a C# programmer, I've had it drummed into me that assigning nil/null to things is pointless. While I can see it makes more sense in Objective-C, it still grates my sense of code aesthetic slightly*. I removed it and it all worked fine.

但是,当我尝试使用 MKMapView 做类似的事情时,尝试加载NIB时出现应用程序错误 EXC_BAD_ACCESS

However, when I tried to do a similar thing with MKMapView the application errors EXC_BAD_ACCESS while trying to load the NIB.

#import <Foundation/Foundation.h>

@interface MyViewController : UIViewController {
    IBOutlet MKMapView *mapView;
}
@end
- (void)viewDidUnload {
    [super viewDidUnload];
    [mapView release];
    mapView = nil; // Without this line an exception is thrown.
}

mapView 未设置为 nil ,但是当 aLabel 未设置为时未设置否

Why is there an error when mapView is not set to nil, but not when aLabel is not set to nil?

*我意识到我需要为新语言调整代码美感,但这需要时间。

* I realise I need to adjust my sense of code aesthetic for the new language, but it takes time.

事实证明,我只是因为没有引用 aLabel 而完全错了。不确定是什么让我觉得不是。

It turns out I was just flat out wrong about aLabel not being referenced. Not sure what made me think it wasn't.

但是,仍然有一个问题,为什么在加载NIB时会引用它们。

However, that still leaves the question of why they're being referenced while the NIB is being loaded.

设置了字段或属性后,将释放消息发送到旧值(综合属性set方法发送该释放消息,或者 setValue: forKey:发送消息(如果是字段)。由于旧值已被释放,因此将导致 EXC_BAD_ACCESS

When the field or property is set, a release message is sent to the old value (either the synthesized properties set method sends the release message, or setValue:forKey: sends the message if it's a field). Because the old value has already been released, this results in EXC_BAD_ACCESS.

推荐答案

viewDidUnload

在View堆栈中,设备有时可能会通过释放不使用的对象来释放内存。想象一下,您有一个包含许多视图控制器的导航堆栈。堆栈中较低的接口视图控制器不可访问,但仍在用尽内存。因此,通常最好清除所有无法访问的界面元素。

In the View stack there may be situations where the device can free up memory by releasing objects that aren't in use. Imagine you have a navigation stack with a number of view controllers. The interface view controllers lower on the stack are not accessible but are still using up memory. So its usually a good idea to nil out any interface elements that can't be accessed. These will then be reloaded with viewDidLoad when needed.

通常在 ViewDidUnload 中,应该释放所有创建的视图对象从Nib文件或在您的 ViewDidLoad 方法中分配

Generally in ViewDidUnload you should release any view objects that are created from a Nib file or allocated in your ViewDidLoad method

您的 mapView 引发异常,因为您的视图控制器试图访问 MapView ,但 mapView 已发布。当您将出口设置为零时,发送给它的所有消息都将被忽略。

Your mapView is throwing an exception because your view controller is trying to access to MapView but the mapView has been released. When you set the outlet to nil any messages sent to it are ignored.

这篇关于为什么在viewDidUnload中将nil分配给IBOutlets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:40