- (IBAction)namePoints:(id)sender {
yValuePoints = 180;
pointTextBoxCounter = 0;
while (numberOfPointsTextBox.text.intValue > currentPointTextBox) {
CGRect textFrame = CGRectMake(245, yValuePoints, 60, 30);
UITextField *textField = [[UITextField alloc] initWithFrame:textFrame];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
textField.textAlignment = UITextAlignmentRight;
[self.view addSubview:textField];
currentPointTextBox += 1;
yValuePoints += 40;
if (yValuePoints > mainScrollView.contentSize.height) {
[mainScrollView setContentSize:CGSizeMake(320, (yValuePoints + 20))];
}
}
while (numberOfPointsTextBox.text.intValue < currentPointTextBox) {
[self.view.subviews.lastObject removeFromSuperview];
//[[pointsTextFieldsArray objectAtIndex:currentPointTextBox] removeFromSuperview];
currentPointTextBox -= 1;
}
}
当numberOfPointsTextBox didFinishEditing时调用此函数。 CurrentPointTextBox是一个int(希望)跟踪当前屏幕上的点文本框的数量(其他诸如具有类似功能的平面)。我想要的是减少numberOfPointsTextBox的值以删除额外的点文本框。我一直在尝试做的是使用pointsTextFieldsArray来跟踪我在self.view.subviews数组中创建的字段的索引值,这样我就可以运行注释掉的代码行,但是NSMutableArray不会接受int值,我找不到动态创建NSIntegers的方法。有谁知道如何做到这一点?或者更好的方法呢?
答案 0 :(得分:1)
使用NSNumber,你可以把它放在NSMutableArray中,因为它是一个对象。
答案 1 :(得分:1)
我相信你的做法并不完全正确。每次更改currentPointTextBox
时,您都应该更新视图。
那就是说,你需要在你的init函数中将它设置为0
(零),并从那里开始。
我假设你总是删除最后一个或添加到“列表”的末尾。这样,您可以将TextField存储在pointsTextFieldsArray中,它应该是NSMutableArray
对象。
我已经整理了一些代码(基于你的代码),这些代码应该指向正确的方向:
-(id) init {
self = [super init];
if (self) {
currentPointTextBox = 0;
}
}
-(void)viewDidLoad {
pointsTextFieldsArray = [[NSMutableArray alloc] init];
}
-(void)setCurrentPointTextBox:(NSInteger)num {
while (currentPointTextBox < num) {
currentPointTextBox++;
// Create TextField
yValuePoints = 180 + 40 * (currentPointTextBox - 1);
UITextField *textField = [[UITextField alloc] initWithFrame:textFrame];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
textField.textAlignment = UITextAlignmentRight;
[pointsTextFieldsArray addObject:textField];
[self.view addSubview:textField];
}
while (currentPointTextBox > num) {
currentPointTextBox--;
UITextField *textField = [pointsTextFieldsArray lastObject];
[textField removeFromSuperView];
[pointsTextFieldsArray removeObject:textField];
}
yValuePoints = 180 + 40 * (currentPointTextBox - 1);
if (yValuePoints > mainScrollView.contentSize.height) {
[mainScrollView setContentSize:CGSizeMake(320, (yValuePoints + 20))];
}
}
如果您需要更多帮助,请再多补充一下。
答案 2 :(得分:1)
您的 pointsTextFieldsArray 存储对象(因此其方法名称为“addObject”),因此如果您想存储基本类型,例如 int 和 float (记住NSInteger和CGFloat只是正确类型的int和float的包装,取决于你是在32位还是64位平台上运行),使用NSNumber包装它们,如
[pointsTextFieldsArray addObject:[NSNumber numberWithInt:someIntVariable]];
请记住 NSArray 不是数组,它是一个保存对象的类。您可以将其视为Java中的List或.NET中的ArrayList。