简体   繁体   English

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

[英]How to set picture for uibutton when accessing it as subview

So i have a button in a view i know how to access it but i have no idea how to change picture on that button. 所以我在视图中有一个按钮,我知道如何访问它,但是我不知道如何更改该按钮上的图片。 Here is what i got: 这是我得到的:

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

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

However that results in a error: 但是,这会导致错误:

No visible @interface for 'UIView' declares the selector 'setImage:forState:'

I'm guessing that's because button is UIView not UIButton, any idea how to do that? 我猜这是因为按钮是UIView而不是UIButton,知道怎么做吗?

You need to cast your pointer so that the compiler recognizes it as being a UIButton and not a generic UIView . 您需要强制转换指针,以便编译器将其识别为UIButton而非通用UIView This is entirely safe since you are checking the runtime type of your pointed object, so you can be sure it is a UIButton . 这是完全安全的,因为您正在检查指针对象的运行时类型,因此可以确保它是UIButton

Use this: 用这个:

        [(UIButton*)button setImage:picture forState:UIControlStateNormal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM