2012-10-25 2 views
0

나는 버튼을 눌렀을 때 UITextField를 만들 때 배열을 사용합니다. 새 단추를 추가하려면 Undo 기능을 사용하십시오. Undo 버튼을 누르면 마지막으로 생성 된 UITextField가 제거됩니다. 내 ViewController.m버튼을 누를 때 어떻게 UITextField를 실행 취소 할 수 있습니까?

#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 

답변

0

같은 것을해야 당신 운데 행동보다

당신이 배열에 텍스트 필드를 추가하고 있기 때문에 그냥 배열에서 마지막 개체를 가져옵니다 그것의 superview에서 그것을 제거하십시오.

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

감사합니다. :) – chineseGirl

0

에 내 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 

각 텍스트 필드에게 그들이 배열에 위치하여 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 

}

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

}

관련 문제