0

私のプロジェクトでは、目的のために以下のコードを使用しましたが、正しく機能しています。私のコード:

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

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

しかし、ここでは UIImageView の代わりに UIButton を使用したいので、上記のコードの最初の行を次のように置き換えました。

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

しかし、この時点でエラーメッセージが表示されます

「互換性のない型 'void' の式で 'UIView*_strong' を初期化しています」.

UIImageViewをUIButtonに置き換えることで、これを解決するのを手伝ってくれる人はいますか....

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 に答える