我正在制作一个在主视图中有2个按钮的程序;
一个叫做show,另一个叫hide,
当用户按下show butoon时,会将imageview添加到屏幕上
代码:
-(IBAction)show{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 155, 155)];
img.image = [UIImage imageNamed:@"icon.png"];
[self.view addSubview:img];
}
Run Code Online (Sandbox Code Playgroud)
当用户按下隐藏按钮时,我希望app隐藏刚刚添加的图像(img)
但...
当我使用
-(IBAction)add{
[img removeFromSuperView];
}
Run Code Online (Sandbox Code Playgroud)
Xcode说"img Undecleared"
编辑:有人说将对象定义为公共对象(@property),但问题是imageview只添加一次.但我希望每次用户按下Show按钮时添加新的imageview,
所以我用[[self subviews] objectAtIndex:xx] removeFromSuperview]方法来解决问题