我有两件事似乎应该很容易,我认为它们很容易,但这是我的第一个Objective-C程序,所以它不像我在我的本地Perl那样容易找到我.这两个例子几乎相同,但我在想,因为一个人使用@synthesize
它可能会有很大不同.
例1
// What Works
@synthesize display0 = _display0;
@synthesize display1 = _display1;
@synthesize display2 = _display2;
@synthesize display3 = _display3;
// What I would like to do:
for (int i=0; i<4; i++)
{
@synthesize display$i = _display$i;
}
Run Code Online (Sandbox Code Playgroud)
例2
// Works
- (IBAction)clearPressed
{
self.display0.text = @"0";
self.display1.text = @"0";
self.display2.text = @"0";
self.display3.text = @"0";
}
// What I would like to see
- (IBAction)clearPressed
{
for (int i=0; i<4; i++) {
self.display$i.text = @"0";
}
}
Run Code Online (Sandbox Code Playgroud)
让我走向正确方向的任何帮助都会很棒!
小智 13
如果您正在使用UILabel,请尝试这样做:
@property (nonatomic, retain) IBOutletCollection(UILabel) NSArray *valueFields;
- (IBAction)clearPressed
{
for(UILabel *label in valueFields)
{
label.text = @"0";
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
只需使用IBOutletCollection:
@property (strong) IBOutletCollection(UILabel) NSArray *labels;
Run Code Online (Sandbox Code Playgroud)
然后你可以使用快速枚举循环它:
UILabel *label;
for (label in labels) {
label.text = @"0";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1168 次 |
最近记录: |