2012-07-16 10 views
0

我的視圖底部有一個文本框。當你點擊編輯它時,鍵盤會覆蓋它,而你看不到你正在輸入的內容。我想創建一個方法,在編輯這個文本字段時重新調整視圖。在代碼中分配一個方法到UITextField

如果我通過界面生成器聲明瞭textfield,那麼我知道我所要做的就是鏈接(按住Ctrl並單擊拖動)它。但我有代碼中聲明的textfield,所以我想知道如何分配一個方法。我想做些什麼

僞代碼:

if (the text field is currently being edited){ 
    call method: adjust view 
} 

回答

3

在您的.h落實UITextFieldDelegate像這樣:

@interface ShippingInfoViewController : UIViewController < UITextFieldDelegate>

,並確保你的文本字段的委託設置爲self

然後,你必須使用許多方法,如

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

-(void) textFieldDidBeginEditing:(UITextField *)textField{

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

- (void)textFieldDidEndEditing:(UITextField *)textField {

等等。

apple reference

+0

在我的。.h文件我有我的接口中聲明如下 '@interface SecondDetailViewController:的UIViewController ' 當我設置文本字段的委託,以自己崩潰在運行時,任何想法? – BloonsTowerDefence 2012-07-16 17:46:22

+0

你最近怎麼樣?是故事板或代碼中的文本框?我通常通過控制點擊(或右鍵單擊)從字段到文件所有者(底部的黃色框)並選擇委託來在故事板中執行操作。在代碼中它應該是'textField.delegate = self;' – jacerate 2012-07-16 18:18:59

+0

我發現了這個問題;我在分配之前設置了代表。儘管感謝您的回覆。 – BloonsTowerDefence 2012-07-16 18:24:13

0

你必須指派一名代表爲文本視圖:

[textView setDelegate: self] 

,我認爲,這self是您的視圖控制器,這需要符合UITextViewDelegate protocol。無論何時開始編輯循環,文本視圖都會調用textViewShouldBeginEditing:/textViewWillBeginEditing

+0

它只是重複什麼已經說過 – jacerate 2012-07-16 17:47:50

+0

@ user1458968這是非常真實的;但我怎麼知道?我的回答是第一個:-)沒關係。 – Dirk 2012-07-16 18:11:22

1

當您創建文本字段時,請將其委託設置爲self。

[textField setDelegate:self]; 

然後實現委託方法

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    //adjust view here 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    //move view back to correct position here 
} 

您也可以註冊UIKeyboardWillShow/UIKeyboardWillHide或UIKeyboardDidShow/UIKeyboardDidHide消息,並實現該功能爲好。

0

使用以下功能:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 


- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    [self moveTextViewForKeyboard:notification up:YES]; 
} 


- (void)keyboardWillHide:(NSNotification *)notification 
{ 
    [self moveTextViewForKeyboard:notification up:NO]; 
} 


- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up { 
    NSDictionary *userInfo = [notification userInfo]; 
    NSTimeInterval animationDuration; 
    UIViewAnimationCurve animationCurve; 
    CGRect keyboardRect; 

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
    animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; 

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    [UIView setAnimationCurve:animationCurve]; 

if (up == YES) { 
    CGFloat keyboardTop = keyboardRect.origin.y; 
    CGRect newTextViewFrame = self.noteTextView.frame; 
    originalTextViewFrame = self.noteTextView.frame; 
    newTextViewFrame.size.height = keyboardTop - self.noteTextView.frame.origin.y - 10; 

    self.noteTextView.frame = newTextViewFrame; 
} else { 
    // Keyboard is going away (down) - restore original frame 
    self.noteTextView.frame = originalTextViewFrame; 
} 

    [UIView commitAnimations]; 
} 

使用originalTextViewFrame靜態變量:

static CGRect originalTextViewFrame; 
相關問題