目前我创建了一个可拖动的 UIView 作为 subview ,当前在每次创建时显示其中的文本。但是,我希望其中的文本随着每次创建而改变,例如第一个 UIView 显示 1,第二个显示 2,第三个显示 3,等等。到目前为止,这是我的代码:
- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
{
UIView *draggedView = panner.view;
CGPoint offset = [panner translationInView:draggedView.superview];
CGPoint center = draggedView.center;
draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
draggedView.layer.borderColor = [UIColor blueColor].CGColor;
draggedView.layer.borderWidth = 4.0f;
// Reset translation to zero so on the next `panWasRecognized:` message, the
// translation will just be the additional movement of the touch since now.
[panner setTranslation:CGPointZero inView:draggedView.superview];
}
}
- (IBAction)addRepButton:(UIBarButtonItem *)newRep {
buttonCount ++;
if (buttonCount > 1 )
{
UILabel *textField = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
textField.userInteractionEnabled = YES;
textField.layer.cornerRadius = 20;
[textField setBackgroundColor: [UIColor whiteColor]];
textField.font = [UIFont systemFontOfSize:20];
textField.layer.borderColor = [UIColor blackColor].CGColor;
textField.textColor = [UIColor blackColor];
textField.layer.borderWidth = 4.0f;
textField.text = @"1";
textField.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:textField];
UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(panWasRecognized:)];
[textField addGestureRecognizer:panner];
}
}
@end
最佳答案
您需要的是该类的一个属性,用于记录标签的数量。所以在接口(interface)(头文件,或者你可以在主文件的顶部添加一个接口(interface))添加一个整数属性,就像这样:
@property (nonatomic, assign) NSInteger labelCounter;
在您的 viewDidLoad 方法中将计数器初始化为零,例如:
self.labelCounter = 0;
现在在您的 addRepButton 方法中,将标签文本设置为该数字的值,如下所示:
textField.text = [NSString stringWithFormat:@"%i", self.labelCounter];
并且在相同的方法中,增加下一个标签的计数器:
self.labelCounter++;
应该可以了。
关于ios - 如何在每次添加时将文本显示为 UIView 的数量作为 ULabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294824/