Objective C如何在类方法中引用视图

Tor*_*ori 0 objective-c uiview ios

我的应用程序有多个视图以编程方式添加按钮,而不是在每个控制器中使用以下实例方法,我想将该方法放在一个公共类中,将其用作类方法.

+ (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];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何引用self.view调用视图.如self.view没有在普通类公知的.

Tim*_*Tim 6

你不能,一个类方法没有概念self.您需要将视图作为参数传递.

+ (void)addLeftImageButton:(UIButton *)leftButton:(float)x:(float)y:(NSString *)name:(int) tag forView:(UIView *)view;
Run Code Online (Sandbox Code Playgroud)