如何在iOS中删除UITextfield并从中输入数据

时间:2015-08-18 06:35:38

标签: ios objective-c uitextfield

我开发了一个应用程序,其中有两个UIButton名为[+]和[ - ],一个UITextField默认。

现在我想要的是当我点击加号 [+] 按钮时,它应该在UITextField中添加一个UIView,这对我来说是完美的,但是当我点击减去[ - ]按钮它应该删除最后创建的UITextField,它现在对我不起作用。

如何管理?

这是我的代码加上[+]按钮。

- (IBAction)btnPlusClicked:(id)sender
{

    scrollViewSpecialitiesTxtFields.hidden = FALSE;

    float y;

    y = (35 * txtSpecialityFieldCounter);

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, 120, 30)];
    textField.delegate = self;
    textField.borderStyle = UITextBorderStyleRoundedRect;

    [arraySpecialitiesTxtFields insertObject:textField atIndex:arraySpecialitiesTxtFields.count];

    scrollViewSpecialitiesTxtFields.contentSize = CGSizeMake(125, y+30);
    [scrollViewSpecialitiesTxtFields addSubview:textField];

    txtSpecialityFieldCounter++;
    NSLog(@"txtSpecialityFieldCounter : %d",txtSpecialityFieldCounter);
}

这是我的减号[ - ]按钮代码

-(IBAction)btnMinusClicked:(id)sender
{
    [scrollViewSpecialitiesTxtFields.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    txtSpecialityFieldCounter--;
    NSLog(@"txtSpecialityFieldCounter : %d",txtSpecialityFieldCounter);

    float y;

    y = (35 * txtSpecialityFieldCounter);

    for(int i = 0 ; i < txtSpecialityFieldCounter; i++)
    {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, 120, 30)];
        textField.delegate = self;
        textField.borderStyle = UITextBorderStyleRoundedRect;

        [arraySpecialitiesTxtFields insertObject:textField atIndex:arraySpecialitiesTxtFields.count];

        scrollViewSpecialitiesTxtFields.contentSize = CGSizeMake(125, y-30);
        [scrollViewSpecialitiesTxtFields addSubview:textField];
    }
}

我知道减号[ - ]按钮代码错误,但让我知道更改

3 个答案:

答案 0 :(得分:1)

NSMutableArray *myAddedTextfields

中创建一个全局变量viewDidLoad-> myAddedTextFields = [NSMutableArray array];
- (IBAction)plusAction:(id)sender 
{
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 100, 20)];

textField1.text = @"hey how r u";

textField1.backgroundColor = [UIColor grayColor];

textField1.tag = 1;
[self.view addSubview:textField1];
[myAddedTextFields addObject:textField1];
}

- (IBAction)minusAction:(id)sender 
{
UITextField *txtField = [myAddedTextFields objectAtIndex:myAddedTextFields.count -1]; //for deleting from the last textfield onwards.

[txtField removeFromSuperView];
[myAddedTextFields removeObjectIdenticalTo:txtField];

}

我已经编辑了你的方法..希望这有帮助。

答案 1 :(得分:1)

每当您想在超级视图上添加UITextField时,请设置该组件的标记(UITextField)。

当你想从superview中删除它时,然后使用标记获取该组件的引用(UITextField)并将其从superview中删除。

以下示例可能对您有所帮助:

-(IBAction)plusAction:(id)sender 
{
    UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 100, 20)];

    textField1.text = @"hey how r u";

    textField1.backgroundColor = [UIColor grayColor];

    textField1.tag = 1;
    [self.view addSubview:textField1];
}

-(IBAction)minusAction:(id)sender 
{
    UITextField * textfield = (UITextField *)[self.view viewWithTag:1];
    [textfield removeFromSuperview];
}

答案 2 :(得分:1)

是的,你可以使用标签选项。或者您可以NSArray使用UITextFields

@interface MyTextFieldViewClass ()

@property (nonatomic, retain) NSMutableArray *textFields;

@end

@implementation MyTextFieldViewClass

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textFields = [NSMutableArray array];
}

- (IBAction)addTextField:(id)sender {
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 20)];

    [self.textFields addObject:tf];
    [self.view addSubview:tf];
}

- (IBAction)removeTextField:(id)sender {
    UITextField *tf = [self.textFields lastObject];
    [tf removeFromSuperview];

    [self.textFields removeLastObject];
}

@end

这样会好一点,因为您不必遍历self.view.subviews中存在的所有视图/ ID。此外,使用tag可能不可靠,因为您总是想要删除最后一项,因此您需要将最后一个项目编号保留在内存中,或者您需要遍历所有子视图以便找到标记最高的UITextField并将其删除。