在viewdidload函数中更改按钮图像

时间:2011-03-11 07:25:45

标签: iphone objective-c

是否可以在viewdidload / viewwillappear函数中更改按钮图像。如果可能,那么特定按钮(如果我有2个按钮)将如何识别这些功能。

3 个答案:

答案 0 :(得分:1)

这是我在viewDidLoad中用来更新天气按钮图像的代码:

UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:iconURL]]] retain];
if (img != nil) 
{ 
    [weatherButton setImage:img forState:normal];
    img = nil; 
}

我从网址获取图片。希望这会有所帮助。

答案 1 :(得分:0)

是的,你可以使用这个

[button setImage:image forState:state];

答案 2 :(得分:0)

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.frame = CGRectMake(275, 8, 40.0, 40.0);
        [button setBackgroundImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"button_home.png"]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(goHome) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [self.view bringSubviewToFront:button];

这里我以编程方式创建了按钮!