在我看来,你提供了一个额外的代码类。我会尽力解释。
首先,在你之前你有两个不同的IBOutlet
。我认为第一个是你添加的。
IBOutlet UISegmentedControl *disp;
第二个,相反,增加了由Xcode中,当你所做的拖放oeration。
@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;
首先考虑的
术语IBOutlet
仅供Xcode中的占位符。通过它,Xcode可以帮助您将实例变量连接到图形元素。
当我使用出口连接我平时喜欢提供一个@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;
访问(如Xcode中所做的那样),因为如果你不能够在内存泄漏问题incurr。
其次考虑
已提供的代码的Xcode是正确的。当您进行拖动操作时,您将使用图形元素链接到displayController
实例变量。为了平衡这方面,实例变量在dealloc方法释放如下所示:
[displayController release];
成瘾,Xcode中因为内存警告的情况下添加[self setDisplaySlider:nil];
viewDidUnload
方法可以调用。这里没有问题,因为当控制器再次加载到内存中时,插座连接被恢复。
两种方法调用之间的区别可以在Advanced Memory Management doc和release-or-set-to-nil-retained-members中读取。请注意,如果你这样做:
[displayController release];
您直接访问您的实例变量称为displayController
,而如果你这样做:
[self setDisplaySlider:nil]; // or self.displaySlider = nil;
您访问的访问(在这种情况下,setter方法)为实例变量。这是不一样的(为了避免混淆,请参阅我提供的代码)。
所以,这是我的代码将使用(我加了一些意见,引导你):
//.h
@interface ViewController : UIViewController
{
UISegmentedControl *disp; // instance variable called disp
// (A) now this is no longer necessary, new compile mechanism will create an instance
// variable called _displaySlider under the hood
}
@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;
@end
//.m
@synthesize displaySlider = disp; // I say to Xcode to create a setter and a getter to access the instance variable called disp as written in @property directive
// no longer necessary for (A)
- (void)viewDidUnload
{
[super viewDidUnload];
[self setDisplaySlider:nil]; // I call the setter method to release the UISegmentedControl
}
- (void)dealloc {
[disp release]; // I release the UISegmentedControl directly
// if you choose the (A) mechanism simply do
// [_displaySlider release];
[super dealloc];
}
希望它能帮助。
我明白,它看起来像XCode生成的代码与我习惯的不同。即使没有@property的标签也会被设置为零并释放。我认为这并不是必要的 – Vikings 2012-01-29 22:13:11