所以我在 View 中有一个按钮,我知道如何访问它,但我不知道如何更改该按钮上的图片。这是我得到的:
for(UIView *button in [okvir subviews]){
if([button isKindOfClass:[UIButton class]]){
UIImage *picture=[UIImage imageWithData:slika];
[button setImage:picture forState:UIControlStateNormal];
}
}
但是这会导致错误:
No visible @interface for 'UIView' declares the selector 'setImage:forState:'
我猜这是因为按钮是 UIView 而不是 UIButton,知道怎么做吗?
最佳答案
您需要转换指针,以便编译器将其识别为 UIButton
而不是通用 UIView
。这是完全安全的,因为您正在检查指向对象的运行时类型,因此您可以确定它是一个 UIButton
。
使用这个:
[(UIButton*)button setImage:picture forState:UIControlStateNormal];
关于iphone - 作为 subview 访问时如何为uibutton设置图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622552/