如何在单击文本字段而不是文本更改时调用方法

时间:2013-02-04 06:43:50

标签: iphone ipad uitextfield

我希望在单击TextField时移动视图,但是当我们更改文本或编写任何内容时,它只适用于textDidChange,以便在单击TextField时调用方法。

 -(void)textFieldTextDidChange:(UITextField*)tf{

    [self  showAnimationPests];


}


    -(void) showAnimationPests{


      [UIView animateWithDuration:0.5 
                 animations:^{
                     self.view.frame = CGRectMake(0,-300,1024,768);
                 }];

     }

而不是我想要在textfield上的textchange点击

在.h文件中

    IBOutlet UIButton*button1;
IBOutlet UIButton*button2;
IBOutlet UIButton*button3;
IBOutlet UIButton*button4;
IBOutlet UIButton*button5;
IBOutlet UIButton*button6;
IBOutlet UIButton*button7;
IBOutlet UIButton*button8;
IBOutlet UIButton*button9;
IBOutlet UIButton*button10;

    @property(nonatomic,retain)IBOutlet UIButton*button1;
    @property(nonatomic,retain)IBOutlet UIButton*button2;
    @property(nonatomic,retain)IBOutlet UIButton*button3;
    @property(nonatomic,retain)IBOutlet UIButton*button4;
    @property(nonatomic,retain)IBOutlet UIButton*button5;
    @property(nonatomic,retain)IBOutlet UIButton*button6;
    @property(nonatomic,retain)IBOutlet UIButton*button7;
    @property(nonatomic,retain)IBOutlet UIButton*button8;
    @property(nonatomic,retain)IBOutlet UIButton*button9;
    @property(nonatomic,retain)IBOutlet UIButton*button10;

4 个答案:

答案 0 :(得分:2)

使用

- (void)textFieldDidBeginEditing:(UITextField *)textField

委托方法。当UITextField成为第一个响应者时调用它。您在文本字段中单击的那一刻将调用此方法。

答案 1 :(得分:1)

您可以使用委托方法

来完成
- (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [self  showAnimationPests];
    }

或者在你的textField中添加tap gasture

- (void)viewDidLoad
    {
        [super viewDidLoad];

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
        [singleTap setNumberOfTapsRequired:1];

        [tf addGestureRecognizer:singleTap];
    }

- (void)handleSingleTap
    {
        [self  showAnimationPests];
    }

答案 2 :(得分:1)

添加此代码:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slideUpView:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slideDownView:) name:UIKeyboardWillHideNotification object:nil];

    -(void)slideDownView:(NSNotification*)notification
{

    [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:animationCurve
                     animations:^{
                         self.view.frame = CGRectMake(0, 0, 320, 480);
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Slide down Done..!");
                     }];
}

-(void)slideUpView:(NSNotification*)notification
{

    [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
    //
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:animationCurve
                     animations:^{
                         self.view.frame = CGRectMake(0, -keyboardFrame.size.height + 70, 320, 416);
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Slide up Done..!");
                     }];
}

答案 3 :(得分:0)

首先在<UITextFieldDelegate>文件中添加.h.m文件中

- (void)viewDidLoad
{
 yourtextfield.delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == yourtextfield)
    {
       [self  showAnimationPests];
    }
}
-(void) showAnimationPests
{
 [UIView animateWithDuration:0.5 
             animations:^{
                 self.view.frame = CGRectMake(0,-300,1024,768);
             }];

 }