iphone - 作为 subview 访问时如何为uibutton设置图片

标签 iphone objective-c ios xcode uiview

所以我在 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/

相关文章:

iphone - 实时应用程序中的核心数据更新

iphone - 如何检测 uitableViewCell 上触摸的按钮并更改该按钮的 BG 图像

objective-c - 在 swift 中使用 MIDIPacketList

iphone - 如何轻松地将 iPhone 应用程序转换为通用应用程序?

ios - X秒后关闭模态ViewController

jquery - 使用 jQuery touchwipe 默认删除某些删除

ios - searchBarTextDidBeginEditing 委托(delegate)方法调用了两次

ios - 嵌套枚举可以保存在 UserDefaults 中吗?

ios - 如果点击 iOS 屏幕上的任何其他地方,是否会关闭菜单?

iphone - 如何让您的 iOS 应用程序接受 QA 测试