当我长按它时,如何删除文本字段?

时间:2012-11-12 03:24:42

标签: xcode xcode4.3

我已经有好几周的问题了。因为我的应用程序需要一个能够创建UITextfield,撤消和删除UITexfield的函数。点击它时,我的代码会在视图的任何位置创建文本字段。并且它还可以撤消按下按钮撤消时创建的最后一个文本字段,它也可以移动,缩放,旋转,但是在我创建了另一个新的文本字段后,旧的文本字段被附加到视图中。这就是为什么当我长按旧文本字段时,只能删除已创建的新文本。我将如何删除旧文本字段?这里是我的代码。

ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>


@interface ViewController : UIViewController<UITextFieldDelegate, UIGestureRecognizerDelegate>
{

    NSMutableArray *textfieldform;//array for textfield
    UITextField *textField1;//a textfield
    CGPoint prevPanPoint;// float for moving the textfield anywhere
    float prevPinchScale;// float for pinching the textfield
    float prevRotation;// float for rotating the textfield
}


@property (nonatomic, retain) NSMutableArray *textfieldform;//array for creating multiple textfield


-(IBAction) undo;
- (IBAction)handleTap2:(UITapGestureRecognizer *)recognizer;
-(IBAction)panGestureAction:(UIPanGestureRecognizer *)pan;
- (IBAction)scaleImage:(UIPinchGestureRecognizer *)recognizer;
- (IBAction)rotateImage:(UIRotationGestureRecognizer *)recognizer;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize textfieldform;
- (void)viewDidLoad{
    [super viewDidLoad];
    //textfieldform = [[NSMutableArray alloc] init];
  // Do any additional setup after loading the view, typically from a nib.
    textfieldform = [NSMutableArray arrayWithCapacity:0];//array of the textfield
}
//connected to the self.view
-(IBAction) longPressGesture:(UIGestureRecognizer*)gesture{
    [textfieldform removeObject:textField1];
    [textField1 removeFromSuperview];
    NSLog(@"baaaaaaam!");
}
//make the textfield move to any direction in the self.view
-(IBAction)panGestureAction:(UIPanGestureRecognizer *)pan {
    CGPoint translation = [pan translationInView:self.view];
    textField1.transform = CGAffineTransformTranslate(textField1.transform, translation.x, translation.y);
    [pan setTranslation:CGPointZero inView:self.view]; 
}

//to make the use of gesture simultaneously within the view
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
//to make the use of gesture simultaneously
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if([touch.view isKindOfClass:[UIControl class]]) {
        return YES;
    }
    return NO;
}
//to pinch the textfield using 2 fingers.
- (IBAction)scaleImage:(UIPinchGestureRecognizer *)recognizer{

    textField1.transform = CGAffineTransformScale(textField1.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
}
//to rotate the textfield using 2 fingers
- (IBAction)rotateImage:(UIRotationGestureRecognizer *)recognizer{
    textField1.transform = CGAffineTransformRotate(textField1.transform, recognizer.rotation);
    recognizer.rotation = 0;
}
//to remove the last textfield that was been created
-(IBAction)undo{
    UITextField *textFieldToRemove = [textfieldform lastObject];
    if (textFieldToRemove) {
        [textfieldform removeObject:textFieldToRemove];
        [textFieldToRemove removeFromSuperview];
    }

}
// for the editing of the textfield 
- (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 == textfieldform.count) {
        textField1 = (UITextField *)[self.view viewWithTag:textfieldform.count];
        [textField1 becomeFirstResponder];
    }
    else {
        [textField resignFirstResponder];
    }
    return YES;
}
//when tap the user can create a textfield on any direction, it create many different textfield. according to how many tap you do on the view
- (IBAction)handleTap2:(UITapGestureRecognizer *)recognizer{
    if (recognizer.state == UIGestureRecognizerStateEnded){
        CGPoint point = [recognizer locationInView:[self view]];
        textField1 = [[UITextField alloc] init];
        textField1.borderStyle = UITextBorderStyleLine;
        [textField1 setAdjustsFontSizeToFitWidth:YES];
        [textField1 setText:@"TextField"];
        CGRect frame ;    
        frame.origin.x = point.x;
        frame.origin.y = point.y; 
        frame.size.width=200;
        frame.size.height=40;
        textField1.frame=frame;
        textField1.autocorrectionType = UITextAutocorrectionTypeNo;
        textField1.keyboardType = UIKeyboardTypeDefault;
        textField1.returnKeyType = UIReturnKeyDefault;
        textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
        textField1.delegate = self;
        textField1.tag = textfieldform.count;
        [textfieldform addObject:textField1];
        [self.view addSubview:textField1];
        [textField1 setAdjustsFontSizeToFitWidth:YES];
    }

}

- (void)viewDidUnload{
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

1 个答案:

答案 0 :(得分:1)

您使用错误的方法DoAction作为选择器,而是使用 LongPressgesture 方法

更新您的代码,如下所示:

- (void)viewDidLoad{
    [super viewDidLoad];

    textfieldform = [[NSMutableArray alloc] init];
    textField1  = ... // Initialize textfield

    UILongPressGestureRecognizer *holdRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressgesture:)];
    [holdRecognizer setMinimumPressDuration:2];
    [holdRecognizer setDelegate:self];
    [textField1 addGestureRecognizer:holdRecognizer];
}

- (void)LongPressgesture:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        //[textfieldform removeObject: recognizer.view];
        [recognizer.view removeFromSuperview];

        NSLog(@"Long press Ended .................");
    }
    else {
        NSLog(@"Long press detected .....................");
    }        
}