【问题标题】:how can I make delete the textfield when i long pressed it.?长按时如何删除文本字段。?
【发布时间】:2012-11-12 03:24:41
【问题描述】:

我已经有这个问题好几个星期了。因为我的应用需要一个函数来创建 UITextfield、撤消和删除 UITextfield。当您点击它时,我的代码会在视图上的任何位置创建文本字段。它还可以撤消按下按钮撤消时创建的最后一个文本字段,它也可以移动、缩放、旋转,但是在我创建另一个新文本字段后,旧文本字段已附加到视图中。这就是为什么当我长按旧文本字段时,它可以删除,只有创建的新文本可以删除。我该怎么做才能删除那个旧的文本字段?这里是我的代码。

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);
}

【问题讨论】:

    标签: xcode xcode4.3


    【解决方案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 .....................");
        }        
    }
    

    【讨论】:

    • 它会删除整个视图。而且我无法再次创建新的文本字段。
    • 我看到您在视图上添加了长按手势。但是你不是像用户长按textfield 那样应该删除吗?
    • 如果是这种情况,请将长按手势添加到 textfield 而不是您的视图
    • 我怎样才能对文本字段本身做出长手势。因为当以编程方式将手势添加到文本字段时,它不接受该手势。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2011-05-08
    • 2020-03-18
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多