gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:46:02 24 4
gpt4 key购买 nike

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

现在我想要的是,当我点击加号 [+] 按钮时,它应该在 UIView 中添加一个 UITextField ,这是完美的我但是当我点击减号 [-] 按钮时,它应该删除最后创建的 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);}

here is my minus [-] button code

-(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];
}
}

我知道 minus[-] 按钮代码是错误的,但让我知道更改

最佳答案

创建一个全局变量作为 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];

}

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

关于ios - 如何删除 UITextfield 并在 iOS 中从中输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com