2012-03-13 52 views
0

我已经创建了一个出口收集几个按钮,现在我需要更改按钮的背景,这里是我的代码,但编译器给我一个错误:添加背景图片直销收藏

[outlet makeObjectsPerformSelector:@selector(setBackgroundImage:img forState:UIControlStateSelected)]; 

错误:

预期 ':'

我该怎么解决呢?

编辑:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]; 
imgView.image = [UIImage imageNamed:@"mask.png"]; 
[outlet makeObjectsPerformSelector:@selector(setBackgroundImage:)withObject:imgView]; 
[imgView release]; 

回答

1

您不能在@selector(...)中放置参数。您需要使用makeObjectsPerformSelector:withObject:,例如你的代码应该是东西

[EsOutlet makeObjectsPerformSelector:@selector(setBackgroundImage:) withObject:img]; 

我说东西喜欢,因为我不认为有一种方法为选择一个接收多于一个参数做到这一点。

+0

谢谢,但应用程序将崩溃!你会看到我编辑的代码吗? – 2012-03-13 16:31:53

+0

“UIButton”没有'setBackgroundImage:'方法,只有'setBackgroundImage:forState:'。你正试图调用一个不存在的方法。这就是为什么我在我的回答中说了一些事情。所以,你应该在这里接受其他海报的建议,然后自己迭代对象。你也可以在类'UIButton'中添加一个'setBackgroundImage:'方法,但这更加先进。 – shawkinaw 2012-03-13 18:36:02

0

你不能有内@selector语句的参数。由于没有makeObjectsPerformSelector需要NSArray中的两个参数,因此您需要遍历对象并自行调用setBackgroundImage:forState:方法。

E.g.

for (UIButton *b in outlet) { 
    [b setBackgroundImage:img forState:UIControlStateSelected]; 
} 
+0

请你看看我编辑的代码? – 2012-03-13 17:34:11

+0

@ Mc.Lover:查看更新后的答案,如何正确执行。 – 2012-03-13 18:46:33

0

这是因为您的选择器语法不正确。您无法向@selector声明提供参数。

既然你需要提供多个参数(图像+控制状态),你应该通过EsOutlet迭代,并执行操作:

for (UIControl *control in EsOutlet) 
{ 
    //perform action on control 
}