0

在我的项目中,我出于某种目的使用了以下代码,并且它工作正常。我的代码:

 UIView *view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",index]]] autorelease];

view.frame = CGRectMake(70, 80, 180, 260);
return view;

但是在这里我想使用 UIButton 而不是 UIImageView,所以我将上面代码的第一行替换为:

  UIView *view=[[UIButton alloc] setBackgroundImage:(UIImage *)[scrollImages objectAtIndex:index] forState:UIControlStateNormal];

但此时我收到一条错误消息

“使用不兼容类型 'void' 的表达式初始化 'UIView*_strong'”。

任何人都可以通过用 UIButton 替换 UIImageView 来帮助我解决这个问题......

4

2 回答 2

0

你应该通过'alloc'创建它之后init的实例UIButton

UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:(UIImage *)[scrollImages objectAtIndex:index] forState:UIControlStateNormal];
UIView *view = button;

You must use an init... method to complete the initialization process

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state什么都不返回(void),它肯定是与 UIView 不兼容的类型。

于 2013-05-08T03:50:49.070 回答
0

像这样...

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(pressed:) forControlEvents:UIControlEventTouchUpInside];

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",index]];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(70, 80, 180, 260);
[self.view addSubview:button];
于 2013-05-08T03:51:23.103 回答