【发布时间】:2014-04-03 09:27:00
【问题描述】:
我是 iOS 编程的新手,我有一个问题..
我现在的情况是,我想让一组按钮根据我给它们的属性“名称”显示图像。一共有三种名称,我希望按钮显示适合名称的图像。
所以在我的按钮类中我设置了这个方法:
-(void) checkName {
if([self.name isEqualToString:@"v60"]){
[recipeImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"v60.png"]]];
}
else if([self.name isEqualToString:@"aeropress"]){
[recipeImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"aeropress.png"]]];
}
else if([self.name isEqualToString:@"chemex"]){
[recipeImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"chemex.png"]]];
}
}
在我的 recipeButton.h 中,我设置了这样的属性:
@property (strong) NSString *name;
在我的 recipeButton.m 中,我合成它是这样的:
@synthesize name;
在我的视图控制器中,我给按钮的 name 属性是这样的:
recipeButton *recipeButton1 = [[recipeButton alloc] init];
[recipeButton1 setTitle:@"Brewology V60" forState:UIControlStateNormal];
[recipeButton1 addTarget:self action:@selector(pushExample)forControlEvents:UIControlEventTouchUpInside];
recipeButton1.name =@"v60";
有人帮帮我吗?图片没有显示,我想我错过了一些非常重要的东西。
【问题讨论】:
-
何时调用
checkName? -
我在 ViewDidLoad 的 recipeButton 类中调用 checkname
-
recipteButton继承自?因为它似乎是一个按钮,并且没有viewDidLoad方法。viewDidLoad甚至被调用了吗? -
哦,是的,我的 recipeButton 没有 viewDidLoad 呵呵,因为它是 UIButton
-
从外部调用
checkName函数,因为我觉得它没有被调用,或者你在错误的地方调用,在你设置name属性后调用它。