我想将动作的目标设置为创建它的类。但是,当我尝试app crashes
时。
Test.h:
#import <Foundation/Foundation.h>
@class Test;
@protocol TestDelegate<NSObject>
@end
@interface Test : NSObject
{
UIView *theView;
}
@property (assign) id<TestDelegate> delegate;
- (id)initWithView:(UIView *)view;
@end
Test.m:
#import "Test.h"
@implementation Test
- (id)initWithView:(UIView *)view
{
if (self=[super init]) {
theView = view;
int size = 60;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"button" forState:UIControlStateNormal];
button.frame = CGRectMake(view.frame.size.width/2 - size, view.frame.size.height/2 - size, size, size);
[view addSubview:button];
}
return self;
}
- (void)buttonAction:(UIButton *)sender
{
theView.backgroundColor = [UIColor blueColor];
}
@end
错误:
EXC_BAD_ACCESS (code=1,address=0x0)
argv char ** 0x27db7d04 0x27db7d04
*argv char * "/var/mobile/Applications/EC1DE795-A5B7-4E8F-AC3D-E42551D1B284/appName.app/appName" 0x27db7da8
**argv char '/' '/'
argc int 1 1