简体   繁体   English

在Xcode 4.2.1中使用Storyboard时取消分配

[英]Dealloc when using Storyboard in Xcode 4.2.1

I found a somewhat related question at iPhone - dealloc - Release vs. nil , but it didn't answer my question. 我在iPhone上发现了一个与之相关的问题-dealloc-Release vs. nil ,但是它没有回答我的问题。

I created a button, a UIScrollview , and a label inside of it in the storyboard. 我在情节UIScrollview创建了一个按钮,一个UIScrollview及其内部的标签。 When I Ctrl-dragged the objects to ViewController.h they created this: 当我按住Ctrl键将对象拖放到ViewController.h时,他们创建了这个:

@interface ViewController : UIViewController{

    __weak IBOutlet UIScrollView *scroller;
}

@property (weak, nonatomic) IBOutlet UIButton *goButton;

@property (weak, nonatomic) IBOutlet UILabel *label;

@property (weak, nonatomic) IBOutlet UIScrollView *scroller;

- (IBAction)pressGo:(id)sender;

@end

And at the top of ViewController.m I have: 在ViewController.m的顶部,我有:

@implementation ViewController

@synthesize goButton;

@synthesize label;

@synthesize scroller;

And in viewDidUnload it created: 并在viewDidUnload创建它:

[self setGoButton:nil];

[self setScroller:nil];

[self setLabel:nil];

scroller = nil;

[super viewDidUnload];

All created automatically. 全部自动创建。 I've seen many references to dealloc and release . 我看过很多关于deallocrelease参考。 Will this code, the premade stuff, prevent memory leaks? 这段代码(预制的东西)可以防止内存泄漏吗? Or must I add dealloc ? 还是必须添加dealloc I apologize for any errors I have made... I'm new to this. 对于所犯的任何错误,我深表歉意。

If IBOutlet are marked as weak, there's no need of setting them to nil in viewDidLoad. 如果IBOutlet被标记为弱,则无需在viewDidLoad中将它们设置为nil。 If you are compiling with ARC there's also no need of writing dealloc, in any case the compiler itself is going to give you error if you write any dealloc or release statement. 如果使用ARC进行编译,则也无需编写dealloc,无论如何,如果编写任何dealloc或release语句,编译器本身都会给您错误。

With Xcode 4 ARC being default option, if you make sure of that your code is memory safe. 如果确定代码是内存安全的,则将Xcode 4 ARC作为默认选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM