我想从包含方法列表的其他类创建一个按钮,但是当我点击按钮时会发生错误。
ClassA.h
@interface ClassA : NSObject
+ (UIButton *)addUIButton:(UIView *)view text:(NSString *)string action:(ClassB *)action;
+ (UITextField *)addUITextFieldInto:(UIView *)view text:(NSString *)string;
ClassA.m
+ (UIButton *)addUIButton:(UIView *)view text:(NSString *)string action:(ClassB *)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(45, 60, 200, 50);
[button setTitle:string forState:UIControlStateNormal];
[button addTarget:action.callerMethod
action:action.selector
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
return button;
}
ClassB.h
@interface ClassB : NSObject
@property (nonatomic) SEL selector;
@property (nonatomic, retain) id delegate;
@property (nonatomic, retain) ClassC *callerMethod;
- (id)initWithDelegate:(id)delegate;
- (void)setAction;
ClassB.m
- (id)initWithDelegate:(id)delegate { //Method Edited
self = [super init];
if (self != nil)
{
// your code here
self.delegate = delegate;
self.callerMethod = [[LKMethod alloc] initWithDelegate:self.delegate];
}
return self;
}
- (void)setAction {
self.selector = self.callerMethod.alertview;
}
ClassC.h
@interface ClassC : NSObject
{
id delegate;
}
@property (nonatomic) SEL alertview;
- (id)initWithDelegate:(id)_delegate;
- (void)alertView;
ClassC.m
- (id)initWithDelegate:(id)_delegate { //Method Edited
self = [super init];
if (self != nil)
{
// your code here
delegate = _delegate;
self.alertview = @selector(alertView);
}
return self;
}
- (void)alertView {
NSLog(@"alertView");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"title"
message:@"message"
delegate:delegate
cancelButtonTitle:@"ok"
otherButtonTitles:nil];
[alert show];
}
我的主要方法
- (void)meth {
ClassB *action = [[ClassB alloc] initWithDelegate:self];
[action setAction];
[ClassA addUIButton:self.view text:@"bigup" action:action];
}
我不知道我能做什么。 错误信息不是很准确..
答案 0 :(得分:2)
对于ClassB和ClassC初始化,如果不调用super或为其赋值,则无法返回self。比如self = [super init]。