我得到了30个这样的代码,具有相同的实现:
// .h
@interface ViewController : UIViewController{
IBOutlet UIImageView *circle;
IBOutlet UIImageView *circle2;
}
@property (nonatomic, retain) IBOutlet UIImageView *circle;
@property (nonatomic, retain) IBOutlet UIImageView *circle2;
// .m
@implementation ViewController
@synthesize circle;
@synthesize circle2;
- (void)viewDidLoad
{
circle = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"Circle.png"]];
circle2 = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"Circle.png"]];
}
在我的代码中的某个地方,我将它添加为一个子视图。
我的问题是,有没有一种方法可以让它变得更短,让它变得可维护。
发布于 2012-05-21 13:51:10
您可以使用一个IBOutletCollection
而不是30个IBOutlets
。不过,您可能希望在每个UIImageView
上设置标记,以便仍然可以区分它们。
(此答案假设您使用笔尖。如果是,请删除在viewDidLoad
中实例化UIImageView
的行。)
发布于 2012-05-21 13:49:46
如果你使用像- Circle1.png
,Circle2.png
这样的名字,那么你可以使用for循环来在循环中创建它。
就像这样-
for (int i = 0; i < imageCount ; i++ ) {
circle = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[NSString stringWithFormat: @"Circle%d.png" , i]]];
}
发布于 2012-05-21 13:50:53
在他们的superview中,你有没有一个模式来放置这些视图?如果是这样的话,您可以使用for循环,该循环转到30,然后以编程方式创建视图并将其添加到其superview中。
举个例子:
for (i = 0; i < 100; i++)
{
UIImageView* circle = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"Circle.png"]];
[self.view addSubview:circle];
}
将添加100个您想要的图像视图。当然,您需要调整循环中每个视图的位置,但这是一般的想法。
https://stackoverflow.com/questions/10680024
复制相似问题