【问题标题】:I want to make a button display an image based on a property set in my view controller.m我想让一个按钮根据我的视图 controller.m 中设置的属性显示图像
【发布时间】: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属性后调用它。

标签: ios iphone methods ios7


【解决方案1】:
-(void) checkName 
{
    [recipeImage setBackgroundImage:[self getImage:@"name Property"] forState:UIControlStateNormal];
}

-(UIImage *)getImage:(NSString *)property
{
    UIImage *image = [UIImage imageNamed:[NSString  stringWithFormat:@"%@.png",property]];
    return image;
}

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2014-03-08
    相关资源
    最近更新 更多