与上一个问题(Creating UI elements programmatically in Objective-C)相关,此方法驻留在项目的一个类中,其他类可以调用它以编程方式创建按钮:
UIButton *createButton(CGFloat x, CGFloat y, CGFloat width, CGFloat height, NSString *caption, NSTextAlignmentCenter textPosition, UIColor *textColor, UIColor *backColor) {
UIButton *control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
[control setTitle:caption forState:UIControlStateNormal];
control.titleLabel.textAlignment = textPosition;
control.backgroundColor = backColor;
[control setTitleColor: textColor forState:UIControlStateNormal];
return control;
}
我现在正在尝试添加目标和操作方法,由常用函数处理,如下所示:
[control addTarget:nil
action:@selector(commonHandler)
forControlEvents:UIControlEventTouchDown];
它适用于与 createButton()函数位于同一类中的按钮,但是虽然按钮也在其他类中成功创建,但它们不会触发 commonHandler()方法。
注意: createButton()和 commonHandler()方法已在其头文件中声明,并由所有类导入。
有没有办法实现这个目标?
编辑:建议的解决方案仍然崩溃
我根据建议修改了代码,并将 createButton 和 buttonAction 转换为Objective-C方法,以启用对 self
class1标题
// class1.h
#import <UIKit/UIKit.h>
#import "class2.h"
@interface class1 : UIViewController {
UIButton *button1;
}
- (void) buttonAction:(UIButton *)control;
- (UIButton *) createButtonWithxPos:(CGFloat)x
yPos:(CGFloat)y
width:(CGFloat)width
height:(CGFloat)height
caption:(NSString *)caption
textPos:(NSTextAlignment)textPosition
textClr:(UIColor *)textColor
backClr:(UIColor *)backColor;
@end
class1 implementation
// class1.m
#import "class1.h"
#import "class2.h"
@implementation class1
- (void)viewDidLoad {
[super viewDidLoad];
button1 = [self createButtonWithxPos:10
yPos:10
width:200
height:30
caption:@"Cls1 Button"
textPos:NSTextAlignmentCenter
textClr:[UIColor whiteColor]
backClr:[UIColor blackColor]];
[self.view addSubview: button1];
}
- (UIButton *) createButtonWithxPos:(CGFloat)x
yPos:(CGFloat)y
width:(CGFloat)width
height:(CGFloat)height
caption:(NSString *)caption
textPos:(NSTextAlignment)textPosition
textClr:(UIColor *)textColor
backClr:(UIColor *)backColor
{
UIButton *control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
[control setTitle:caption forState:UIControlStateNormal];
control.titleLabel.textAlignment = textPosition;
control.backgroundColor = backColor;
[control setTitleColor: textColor forState:UIControlStateNormal];
[control addTarget:self action: @selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
return control;
}
- (void) buttonAction:(UIButton *)control {
NSLog(@"button clicked...");
}
@end
class2标题
// class2.h
#import <UIKit/UIKit.h>
@interface class2 : UIViewController {
UIButton *button2;
}
@end
class2 implementation
// class2.m
#import "class1.h"
#import "class2.h"
@implementation class2
- (void) viewDidLoad {
[super viewDidLoad];
class1 *cls1 = [[class1 alloc] init];
button2 = [cls1 createButtonWithxPos:10
yPos:10
width:200
height:30
caption:@"Cls2 Button"
textPos:NSTextAlignmentCenter
textClr:[UIColor whiteColor]
backClr:[UIColor blackColor]];
[self.view addSubview: button2];
}
@end
button1 和 button2 都已正确创建,但 button1 会在按钮2时触发 buttonAction 方法没有 - 它在触摸时崩溃。
是否实施错误?
(请原谅任何遗漏,因为我只从实际项目中提取了相关部分,以便在此说明)