我想通过按下按钮创建一个UIImageView。我是这样做的:
- (IBAction) addPicture:(id)sender {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
imageView.image = [UIImage imageNamed:@"Picture.png"];
[self.view addSubview:imageView];
}
我在.h中“创建”了这个imageView,因为我想在其他方法中使用ImageView。如何使用IBAction在方法中创建无限的UIImageViews,我可以在以后使用其他方法等等...每个imageView必须得到一个名字,或者?
例如,在我的app-idea中,用户可以使用按钮创建图像,然后他选择带触摸的图片并可以移动它并调整其大小。
我熟悉UIImageViews等的创建,因为到目前为止我只使用了预定义的UIImageViews(带有IB)等等。我希望得到一个有用的答案。感谢
答案 0 :(得分:3)
UIView及其子类具有tag
属性用于此目的:
(IBAction) addPicture:(id)sender {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
imageView.tag = 100;
imageView.image = [UIImage imageNamed:@"Picture.png"];
[self.view addSubview:imageView];
[imageView release]; // Do not forget to release your object!
}
之后,您可以在视图控制器中的任何位置访问UIImageView对象:
UIImageView* imView = (UIImageView*)[self.view viewWithTag:100];
答案 1 :(得分:0)
也许你应该这样做
NSMuteableArray *arrayOfImageViews = [[NSMuteableArray alloc] init];
//when you are ready to create your image views
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
imageView.tag = x; //some unique number for this image view.
[arrayOfImageViews addObject:imageView];
[self.view addSubview:imageView];
[imageView release];
//you can then create the next view and so on
//to reference the view later you can do either one of these things
//where x is the index of the image view.
UIImageView *imageView = (UIImageView *)[arrayOfImageViews objectAt:x];