Iphone 按下按钮时如何撤消UITextField?
当我按下按钮时,我正在使用数组创建UITextField。我想添加一个新的按钮,撤销功能。当我按下“撤消”按钮时,我创建的最后一个UITextField将被删除 在我的ViewController.h上Iphone 按下按钮时如何撤消UITextField?,iphone,objective-c,Iphone,Objective C,当我按下按钮时,我正在使用数组创建UITextField。我想添加一个新的按钮,撤销功能。当我按下“撤消”按钮时,我创建的最后一个UITextField将被删除 在我的ViewController.h上 #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextFieldDelegate> { NSMutableArray *textfieldform; UIT
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
{
NSMutableArray *textfieldform;
UITextField *textField1;
}
@property (nonatomic) NSInteger text1;
@property (nonatomic, retain) NSMutableArray *textfieldform;
@property (nonatomic, readwrite) int yOrigin;
@property (nonatomic, readwrite) int xOrigin;
-(IBAction) textFieldcreating;
-(IBAction) undo;
@end
根据文本字段在数组中的位置,为每个文本字段指定一个标记
-(IBAction)textFieldcreating{
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(xOrigin, yOrigin, 100, 40)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.font = [UIFont systemFontOfSize:15];
textField1.placeholder = @"enter text";
textField1.autocorrectionType = UITextAutocorrectionTypeNo;
textField1.keyboardType = UIKeyboardTypeDefault;
textField1.returnKeyType = UIReturnKeyDone;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField1.delegate = self;
textField1.tag = textfieldform.count;
[textfieldform addObject:textField1];
[self.view addSubview:textField1];
yOrigin = yOrigin + 40 + 10;
xOrigin = xOrigin + 20 + 10;
//old yorigin + btn height + y offset
}
在你的行动中应该有
-(IBAction)undo{ //Should also check here if you actually have an object at that index path. (for example if there are no text fields created yet)
UITextField *textField = (UITextField *)[self.view viewWithTag:textfieldform.count - 1];
textField = nil;
[textField removeFromSuperview];
[textfieldform removeLastObject];
}因为要将文本字段添加到数组中,所以只需从该数组中获取最后一个对象并将其从superview中删除即可
- (IBAction)undo:(id)sender {
UITextField *textFieldToRemove = [textfieldform lastObject];
if (textFieldToRemove) {
[textfieldform removeObject:textFieldToRemove];
[textFieldToRemove removeFromSuperview];
}
}
- (IBAction)undo:(id)sender {
UITextField *textFieldToRemove = [textfieldform lastObject];
if (textFieldToRemove) {
[textfieldform removeObject:textFieldToRemove];
[textFieldToRemove removeFromSuperview];
}
}