簡體   English   中英

UITextField鍵盤問題

[英]UITextField keyboard Issue

我有這樣一個問題:我的UITableViewCell有一個UITextField 當我點擊該文本字段時->鍵盤出現,但是當我按Enter按鈕時鍵盤不會消失。 我的文本字段和鍵盤需要這樣的行為:

  1. 當我按Enter時Esc-鍵盤必須消失。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

其中textField是UITableViewCell中的UITextField

嘗試這個


[txtField setReturnKeyType:UIReturnKeyDone];

txtField.enablesReturnKeyAutomatically=YES;

在textField的委托中實現textFieldShouldReturn:方法,然后在其中調用[textField resignFirstResponder] -在按下回車鍵時將隱藏鍵盤。

我不確定這是否也適用於“ Esc”,但是實際設備上都沒有這樣的密鑰,因此這一定不是問題

這可能是舊帖子,但我發現它在尋找答案,因此可能會有其他人這樣做,所以不要開槍射擊我。

只是想添加,別忘了在IB中為UITextField建立委托連接

編寫此代碼以創建UITextLabel

    UITextField *username = [[UITextField alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 110.0f, 30.0f)]
    [username setReturnKeyType:UIReturnKeyNext];
    [username setDelegate:self];
    [self.view addSubview:username];

現在辭職寫這段代碼。

-(void)resignKeyboard
{
    if([username isEditing])
    {
        [username resignFirstResponder];
    }
}

我希望這個對你有用。

嘗試這個

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

或這是在視圖中的任何地方

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@interface Untitled2ViewController : UIViewController <UITextFieldDelegate>
{
    IBOutlet UITextField *text;
}

@property (nonatomic, retain) IBOutlet UITextField *text;
@end

// m文件

#import "Untitled2ViewController.h"

@implementation Untitled2ViewController
@synthesize text;

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

在xib文件中,我設置了:返回鍵:完成,自動啟用返回鍵。 我也嘗試過沒有它,仍然沒有反應。 鍵盤不要隱藏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM