我已经编写了以下代码然后运行。之后,当我触摸 uibutton 时,该应用程序被终止。
我想知道为什么终止。
我怀疑是否自动释放?
有没有人可以明确解释
为什么 myClass 实例被释放和
myClass 被释放的地方和
myClass 可以使用自动释放的方式?
@interface MyClass : NSObject
- (void)testMethod;
@end
@implementation MyClass{
NSMutableArray *array;
}
- (id)init{
if ((self = [super init])) {
array = [[NSMutableArray alloc] initWithCapacity:0];
}
return self;
}
- (void)dealloc {
[array release];
[super dealloc];
}
- (void)testMethod {
NSLog(@"after init : %@", array);
}
@end
@implementation ViewController {
MyClass *myClass;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myClass = [[[MyClass alloc] init] autorelease]; <== cause of ternimate?
UIButton *aButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton addTarget:self action:@selector(testArray:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame=CGRectMake(110.0f, 129.0f, 100.0f, 57.0f);
[aButton setTitle:@"title" forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
[self.view addSubview:aButton];
}
- (void)testArray:(id)sender {
[myClass testMethod];
}
@end