1

私はすでに何週間もこの問題を抱えています。私のアプリには、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);
}
4

1 に答える 1

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 .....................");
    }        
}
于 2012-11-14T00:40:48.280 に答える