繁体   English   中英

满足特定条件时,如何删除在运行时创建的对象?

[英]How do I remove objects created at runtime when a certain condition is met?

- (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的方法。 有谁知道如何做到这一点? 或者更好的方法呢?

使用NSNumber,它可以放在NSMutableArray中,因为它是一个对象。

我相信你的方法不太正确。 每次更改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))];
  }

}

如果您需要更多帮助,请多烦我一点。

您的pointsTextFieldsArray存储对象(因此其方法名称为“ addObject”),因此,如果要存储诸如intfloat的原始类型(请记住,NSInteger和CGFloat只是对int和float的正确类型的包装,具体取决于您是否在32位或64位平台上运行),使用NSNumber包装它们,如同

[pointsTextFieldsArray addObject:[NSNumber numberWithInt:someIntVariable]];

请记住, NSArray不是数组 ,它是一个保存对象的类。 您可以将其视为Java中的List或.NET中的ArrayList。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM