2012-11-07 50 views
0

我有一個程序,當我點擊視圖時,會出現UITextField。我也有一個撤消按鈕。當我雙擊UITextfield它可以被刪除時,我想做一個刪除功能。請幫幫我。如何刪除UITextfield點擊?

這裏是我的代碼:

ViewController.h

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


@interface ViewController : UIViewController<UITextFieldDelegate, UIGestureRecognizerDelegate> 
{ 

    NSMutableArray *textfieldform; 
    UITextField *textField1; 
    float angle; 
    CGFloat beginX; 
    CGFloat beginY; 
    IBOutlet UIView *blahBlah; 
    CGPoint prevPanPoint; 
    float prevPinchScale; 
    float prevRotation; 
} 

@property (nonatomic, retain) NSMutableArray *textfieldform; 

-(IBAction) undo; 
- (IBAction)handleTap2:(UITapGestureRecognizer *)recognizer; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize text1, 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]; 
    angle = 180; 

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)]; 
    [blahBlah addGestureRecognizer:pinchGesture]; 

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)]; 
    [blahBlah addGestureRecognizer:rotationGesture]; 

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)]; 
    [pan setMaximumNumberOfTouches:1]; 
    [pan setMinimumNumberOfTouches:1]; 
    [blahBlah addGestureRecognizer:rotationGesture]; 

    UITapGestureRecognizer *twoFingersTwoTaps = 
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] ]; 
[twoFingersTwoTaps setNumberOfTapsRequired:2]; 
[twoFingersTwoTaps setNumberOfTouchesRequired:2]; 
[blahBlah addGestureRecognizer:twoFingersTwoTaps]; 

} 


- (void)twoFingersTwoTaps { 
    NSLog(@"Action: Delete text, two taps"); 
} 

-(void)panGestureAction:(UIPanGestureRecognizer *)pan { 

    if (pan.state == UIGestureRecognizerStateBegan){ 
     prevPanPoint = [pan locationInView:blahBlah]; 
    } 

    CGPoint curr = [pan locationInView:blahBlah]; 

    float diffx = curr.x - prevPanPoint.x; 
    float diffy = curr.y - prevPanPoint.y; 

    CGPoint centre = textField1.center; 
    centre.x += diffx; 
    centre.y += diffy; 
    textField1.center = centre; 

    prevPanPoint = curr; 
} 


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
    return YES; 
} 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ 

    if([touch.view isKindOfClass:[UIView class]]) { 
     return YES; 
    } 
    return NO; 
} 

- (void)scaleImage:(UIPinchGestureRecognizer *)recognizer 
{ 
    if (recognizer.state == UIGestureRecognizerStateBegan) 
     prevPinchScale = 1.0; 

    float thisScale = 1 + (recognizer.scale-prevPinchScale); 
    prevPinchScale = recognizer.scale; 
    textField1.transform = CGAffineTransformScale(textField1.transform, thisScale, thisScale); 
} 

- (void)rotateImage:(UIRotationGestureRecognizer *)recognizer 
{ 
    if([recognizer state] == UIGestureRecognizerStateEnded) { 

      prevRotation = 0.0; 
    } 

    float thisRotate = recognizer.rotation - prevRotation; 
    prevRotation = recognizer.rotation; 
    textField1.transform = CGAffineTransformRotate(textField1.transform, thisRotate); 
} 


-(IBAction)undo{ 
    UITextField *textFieldToRemove = [textfieldform lastObject]; 
    if (textFieldToRemove) { 
     [textfieldform removeObject:textFieldToRemove]; 
     [textFieldToRemove 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 == textfieldform.count) { 
     textField1 = (UITextField *)[self.view viewWithTag:textfieldform.count]; 
     [textField1 becomeFirstResponder]; 
    } 
    else { 
     [textField resignFirstResponder]; 
    } 

    return YES; 
} 

- (IBAction)handleTap2:(UITapGestureRecognizer *)recognizer{ 

    if (recognizer.state == UIGestureRecognizerStateEnded){ 
     CGPoint point = [recognizer locationInView:[self view]]; 
     textField1 = [[UITextField alloc] init]; 
     textField1.borderStyle = UITextBorderStyleLine; 
     textField1.font = [UIFont systemFontOfSize:15]; 

     CGRect frame ;  
     frame.origin.x = point.x; 
     frame.origin.y = point.y; 
     frame.size.width=300; 
     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; 
     textField1.font = [UIFont fontWithName:@"Arial" size:30]; 
     [textfieldform addObject:textField1]; 
     [blahBlah addSubview:textField1]; 

     [textField1 addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 

    } 

} 

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event{ 
    // get the touch 
    UITouch *touch = [[event touchesForView:textField1] anyObject]; 

    // get delta 
    CGPoint previousLocation = [touch previousLocationInView:textField1]; 
    CGPoint location = [touch locationInView:textField1]; 
    CGFloat delta_x = location.x - previousLocation.x; 
    CGFloat delta_y = location.y - previousLocation.y; 

    // move button 
    textField1.center = CGPointMake(textField1.center.x + delta_x,textField1.center.y + delta_y); 

} 

- (void)moveImage:(UIPanGestureRecognizer *)recognizer 
{ 
    CGPoint newCenter = [recognizer translationInView:self.view]; 
    if([recognizer state] == UIGestureRecognizerStateBegan) { 
     beginX = textField1.center.x; 
     beginY = textField1.center.y; 
    } 
    newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y); 
    [textField1 setCenter:newCenter]; 
} 

- (void)viewDidUnload{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 
+0

首先:您所需的UITextfield行爲聽起來像您不應該在iOs應用程序中使用它,因爲用戶不熟悉它。我現在無法編譯您的代碼,因爲我在iPad上,但您卡在哪裏?是雙擊還是Textfiels的「刪除進度」? –

+0

刪除部分文本字段。並且在我添加一個新的文本框後,我想出了舊的文本框正在附加到視圖。我想讓文本框不會僅僅在視圖上浮動。這樣我可以移動舊的文本框。 – chineseGirl

+0

這聽起來像是你在那裏發明的一些非常瘋狂的UI。 ;-)你有沒有考慮設置它隱藏? –

回答

0
UITextField *textFieldToRemove = [textfieldform objectAtIndex:0]; 
      if (textFieldToRemove) { 
       NSLog(@"baaaaaaam! remove %i", textfieldform.count); 
       [textfieldform removeObject:textFieldToRemove]; 
       [textFieldToRemove removeFromSuperview]; 
      } 

我有點想通了。 :)