![](/img/trans.png)
[英]Assigning a callback to a UIView's UIButton from the containing UIViewController
[英]Assigning UIButton to UIView in Xcode
在我的項目中,我出於以下目的使用了以下代碼,並且該代碼正常工作。 我的代碼:
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'”。
任何人都可以通過將UIImageView替換為UIButton來幫助我解決此問題。
通過“ alloc”創建UIButton
實例后,應將其init
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不兼容的類型。
像這樣...
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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.