我寫了下面的代碼然後運行。 之後,當我觸摸uibutton時,此應用程序終止。爲什麼這段代碼被終止?
我想知道爲什麼終止。
我懷疑是否autorelease?
是否有任何人誰可以明確地解釋
爲什麼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
這絕對是** **不是一個理由,爲什麼你的程序終止,但你應該改變'UIControlStateNormal&UIControlStateHighlighted&UIControlStateSelected'使用'OR's取代'AND's。 – dasblinkenlight