uibutton作为子视图访问时如何设置图片

gab*_*jan 0 iphone xcode objective-c uiview ios

所以我在视图中有一个按钮,我知道如何访问它,但我不知道如何更改该按钮上的图片.这是我得到的:

for(UIView *button in [okvir subviews]){

        if([button isKindOfClass:[UIButton class]]){
                    UIImage *picture=[UIImage imageWithData:slika];
                    [button setImage:picture forState:UIControlStateNormal];
                }
            }
Run Code Online (Sandbox Code Playgroud)

但是会导致错误:

No visible @interface for 'UIView' declares the selector 'setImage:forState:'
Run Code Online (Sandbox Code Playgroud)

我猜这是因为按钮是UIView而不是UIButton,任何想法如何做到这一点?

ser*_*gio 5

您需要转换指针,以便编译器将其识别为a UIButton而不是泛型UIView.这是完全安全的,因为您正在检查指向对象的运行时类型,因此您可以确定它是一个UIButton.

用这个:

        [(UIButton*)button setImage:picture forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)