【问题标题】:Adding array of images to UIbuttons将图像数组添加到 UIbuttons
【发布时间】:2011-06-15 20:08:11
【问题描述】:

我的表格视图中有一些 uiButton,我想用从数组中获取的图像加载它们。但是我无法使用数组将图像添加到按钮,并且它使我的整个应用程序崩溃。任何人都可以为我提供如何使用图像数组将图像添加到表格视图中的按钮的解决方案。任何帮助将不胜感激。

这个方法是从一个循环中调用的,我将一个按钮作为参数传递

-(void)setImage1:(UIButton *)button
UIButton *newButton=[[UIButton alloc]init]; 
newButton=button; 

[newButton setImage:[NSString stringWithFormat:@"%d.png", j] forState:UIControlStateNormal]; j++; } 

谢谢, 克里斯蒂

【问题讨论】:

    标签: objective-c xcode cocoa-touch ipad uitableview


    【解决方案1】:
    //Create an image
    UIImage *image= [UIImage imageWithContentsOfFile:@"image.jpg"];
    
    // Set image for the button
    [button setBackgroundImage:image forState:UIControlStateNormal];
    

    至少向我们展示一些代码以获得更多帮助。

    您不应该在该方法中创建按钮。在 ViewDidLoad 中创建它,或者更好的是,在 Interface builder 中创建。如果您以编程方式创建按钮,则还必须确保启用用户交互,然后设置其框架并将其添加到视图中。

    【讨论】:

    • 再次向我们展示一些代码,以便我们更好地理解。我不明白你到底想做什么,但对我来说这似乎是一个很容易的问题。
    • -(void)setImage1:(UIImageView *)imageView { UIButton *newButton=[[UIButton alloc]init];新按钮=按钮; //[newButton setImage:[NSString stringWithFormat:@"%d.png", j] forState:UIControlStateNormal]; j++; }
    • -(void)setImage1:(UIButton *)button,这是我调用设置图像的方法
    • 每次 j 递增时,我都会为 newButton 设置一个新图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2021-06-20
    • 2011-09-28
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多