2013-02-04 33 views
0

我想在單擊TextField時移動視圖,但當我們更改文本或編寫任何內容時,它只能在textDidChange上工作,因此當單擊TextField時如何調用方法。如何調用點擊文本文件時的方法比文本更改

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

    [self showAnimationPests]; 


} 


    -(void) showAnimationPests{ 


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

    } 

代替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; 
+0

不能清楚地理解你想要什麼..? –

+0

開始接受正確和有用的答案,否則人們將開始忽視給你答案。 – Tirth

回答

2

使用

- (void)textFieldDidBeginEditing:(UITextField *)textField 

委託方法。當UITextField成爲第一響應者時被調用。當你點擊你的文本框時,這個方法會被調用。

+0

感謝Rushi它的工作可以幫助我一件事情,當改變視圖框架的位置,然後視圖的內容也改變了,所以如何修復這些東西 – user1808433

+0

@ user1808433:沒有得到你。您希望將視圖內容移動到視圖中,或者您不想在視圖移動時移動視圖內容? – Rushi

+0

我想同時移動 – user1808433

1

您可以通過委託方法

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

或加自來水gasture到您的文本框

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

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

     [tf addGestureRecognizer:singleTap]; 
    } 

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

Firstofall在.m文件添加<UITextFieldDelegate>.h文件比 做

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

} 
1

加此C ode:

[[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..!"); 
        }]; 
}