Xcode 如何在点击时删除UITextfield?
我有一个程序,当我点击视图时,会出现Xcode 如何在点击时删除UITextfield?,xcode,uitextfield,xcode4.3,uigesturerecognizer,gesture,Xcode,Uitextfield,Xcode4.3,Uigesturerecognizer,Gesture,我有一个程序,当我点击视图时,会出现UITextField。我还有一个撤销按钮。我想在双击UITextfield时创建一个delete函数,它可以被删除。请帮帮我 这是我的密码: ViewController.h #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface ViewController : UIViewController<UITextFieldDelegate, UI
UITextField
。我还有一个撤销按钮。我想在双击UITextfield
时创建一个delete函数,它可以被删除。请帮帮我
这是我的密码:
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
我有点明白了。:) 首先:您想要的UITextfield行为听起来不应该在iOs应用程序中使用它,因为用户不熟悉它。我现在不能编译你的代码,因为我在iPad上,但你被困在哪里了?是双击还是文本字段的“删除进度”?文本字段的删除部分。我还发现,在我添加了一个新的textfield之后,旧的textfield被附加到视图中。我想让文本字段不附加,只是在视图上浮动。这样我就可以移动旧的文本字段。这听起来像是你在那里发明的一些非常疯狂的用户界面你考虑过将其设置为隐藏吗?是的,我甚至尝试过,但它不起作用。这不起作用?:[textField setHidden:yes];是否已将UITextfield连接到IBOutlet?
#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
UITextField *textFieldToRemove = [textfieldform objectAtIndex:0];
if (textFieldToRemove) {
NSLog(@"baaaaaaam! remove %i", textfieldform.count);
[textfieldform removeObject:textFieldToRemove];
[textFieldToRemove removeFromSuperview];
}