Ios 我不能使用我写的UIButton方法
我想写一个按钮的方法,这样我就可以设置一行代码的图像和标题,但它不能被使用Ios 我不能使用我写的UIButton方法,ios,objective-c,Ios,Objective C,我想写一个按钮的方法,这样我就可以设置一行代码的图像和标题,但它不能被使用 +(id)init { UIView *myLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)]; myLeftView.backgroundColor=[UIColor colorW
+(id)init {
UIView *myLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)];
myLeftView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
UIButton *accessButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, myLeftView.bounds.size.width, 44)];
[self setUpButton:accessButton withImage:@"Menu_Avatar" title:@"access"];
}
-(void)setUpButton:(UIButton *)button withImage:(NSString *)imageName title:(NSString *)title {
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[self.myLeftView addSubview:button];
}
这是我在UIView类中编写的方法。更改此方法:
-(instancetype)init{
if (self == [super init]) {
UIView *myLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)];
myLeftView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
UIButton *accessButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, myLeftView.bounds.size.width, 44)];
// [accessButton setImage:[UIImage imageNamed:@"Menu_Avatar"] forState:UIControlStateNormal];
// [accessButton setTitle:@"access" forState:UIControlStateNormal];
[self setUpButton:accessButton withImage:@"Menu_Avatar" title:@"access"];
}
return self ;
}
问题是,不能从类函数调用self 如果您查看init函数声明,它是带+,这意味着它是一个类函数 你可以做两件事: 将init函数保持为类函数,并将下面的函数更改为类函数2
+(id)init {
UIView *myLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)];
myLeftView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
UIButton *accessButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, myLeftView.bounds.size.width, 44)];
[NAMEOFTHECLASS setUpButton:accessButton withImage:@"Menu_Avatar" title:@"access"];
}
+(void)setUpButton:(UIButton *)button withImage:(NSString *)imageName title:(NSString *)title {
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[self.myLeftView addSubview:button];
}
将类的名称更改为类的名称
另一件事是将init函数更改为instance函数。请执行以下操作:
-(id)init {
UIView *myLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)];
myLeftView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
UIButton *accessButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, myLeftView.bounds.size.width, 44)];
[self setUpButton:accessButton withImage:@"Menu_Avatar" title:@"access"];
}
您在+idinit中有一个类方法+并尝试在其中使用self作为实例方法-in-the-void。。。。此外,init不清楚它做什么,也不尊重各种init方法的命名法。