简体   繁体   English

按下按钮时如何撤消UITextField?

[英]How can I Undo UITextField when i press a button?

Im using array on creating an UITextField when i pressed a button. 我在按下按钮时在创建UITextField时使用数组。 I want to add a new button, Undo function. 我想添加一个新按钮,撤消功能。 when i pressed Undo button, the last UITextField I created will be removed. 当我按下“撤消”按钮时,我创建的最后一个UITextField将被删除。

On my ViewController.h 在我的ViewController.h上

#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

On my ViewController.m 在我的ViewController.m上

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize text1, textfieldform, yOrigin, xOrigin;


- (void)viewDidLoad
{
    [super viewDidLoad];
    //textfieldform = [[NSMutableArray alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
    textfieldform = [NSMutableArray arrayWithCapacity:0];
    yOrigin = 0;
    xOrigin = 10;

}


-(IBAction)textFieldcreating{

    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;

    [textfieldform addObject:textField1];
    [self.view addSubview:textField1];
    yOrigin = yOrigin + 40 + 10; 
    xOrigin = xOrigin + 20 + 10; 
    //old yorigin + btn height + y offset

}

-(IBAction)undo{
    if ([textfieldform count]>0) {
        [textfieldform removeLastObject];
        [textField1 removeFromSuperview];
    }
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 

    NSLog(@"textFieldShouldBeginEditing");
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{           

    NSLog(@"textFieldDidBeginEditing");
    [textField1  setBackgroundColor:[UIColor colorWithRed:(248/255.0) green:(248/255.0) blue:(255/255.0) alpha:1.0]];
    textField1.borderStyle = UITextBorderStyleRoundedRect;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    NSLog(@"textFieldShouldEndEditing");
    textField.backgroundColor = [UIColor clearColor];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

    NSLog(@"textFieldDidEndEditing");
    [textField1  setBackgroundColor:[UIColor clearColor]];
    textField1.borderStyle = UITextBorderStyleNone;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSLog(@"textField:shouldChangeCharactersInRange:replacementString:");

    if ([string isEqualToString:@"#"]) {
        return NO;
    }
    else {
        return YES;
    }

}
- (BOOL)textFieldShouldClear:(UITextField *)textField{

    NSLog(@"textFieldShouldClear:");
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSLog(@"textFieldShouldReturn:");

    if (textField.tag == 1) {
        textField1 = (UITextField *)[self.view viewWithTag:2];
        [textField1 becomeFirstResponder];
    }
    else {
        [textField resignFirstResponder];
    }

    return YES;
}




@end

Give each textfield a tag according the the indexPath where they are located in the array. 根据数组中每个文本字段的indexPath为其分配一个标签。

-(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

} }

Than in your unde action should have something like 比您的不定行为应该有类似

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

} }

Since you are adding your textFields to an array just get the last object from that array and remove it from its superview. 由于您将textFields添加到数组中,因此只需从该数组中获取最后一个对象并将其从其超级视图中删除即可。

- (IBAction)undo:(id)sender {
    UITextField *textFieldToRemove = [textfieldform lastObject];
    if (textFieldToRemove) {
        [textfieldform removeObject:textFieldToRemove];
        [textFieldToRemove removeFromSuperview];
    }
}

暂无
暂无

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

相关问题 长按UILabel时,是否可以像UITextField(UITextView,UIWebView)一样在UILabel上实现“选择”和“复制”,怎么办? - Can I implement “select” and “copy” on UILabel when it has a long press just as UITextField (UITextView、UIWebView), and how? 如何在UITextField中移动清除按钮? - How can I move the clear button in a UITextField? 在 iOS 4.3 中,当您按下主页按钮或按下开/关按钮时,我如何区分背景模式? - in iOS 4.3, how i can differentiate between the background mode, when you press the home button or when you press the on/off button? 当我按下编辑按钮时,如何淡出单元格上的文本? - How can I fade out text on a cell when I press edit button? 用户按下主屏幕按钮时,如何使我的广播流应用程序继续唱歌? - How can I make my Radio Streaming Application keep singing when the user press the home button? 如何在编辑时更改UITextField中的颜色 - How can I change color in UITextField when editing 如何使用UITextfield调用“单击时” UITableview - How can I use a UITextfield to call “when clicked” a UITableview 如何添加单击键盘时引发的uitext字段 - How can I add a uitextfield that is raise when keyboard is clicked 当我按一个附件类型删除按钮时,如何删除一个UITableView单元格? - How to delete a UITableView Cell when i press a accessoryType delete button? 如何在按钮上执行长按事件回调 - How can i get a callback for a long press event performed on button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM