我的应用程序有多个视图以编程方式添加按钮,而不是在每个控制器中使用以下实例方法,我想将该方法放在一个公共类中,将其用作类方法.
+ (void) addLeftImageButton:(UIButton *)leftButton:(float)x:(float)y:(NSString *)name:(int) tag { leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [leftButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [leftButton setTag: tag]; [leftButton setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; leftButton.frame = CGRectMake(x, y, 345.0, 345.0); [self.view addSubview:leftButton]; }
我的问题是如何引用self.view
调用视图.如self.view
没有在普通类公知的.
你不能,一个类方法没有概念self
.您需要将视图作为参数传递.
+ (void)addLeftImageButton:(UIButton *)leftButton:(float)x:(float)y:(NSString *)name:(int) tag forView:(UIView *)view;