0

iPhoneアプリ:

  • 名前、メールアドレス、場所を入力するための3つのテキストフィールドがある画面があります。
  • 私は2つのセクションを持つグループ化されたテーブルビューを使用しました。
  • tatの最初のセクションには3つのセルがあります。各セルにはテキストフィールドのラベルがあります。例:名前と呼ばれるラベルと名前を入力するためのテキストボックス。

インターフェイスでは、プロパティではなく変数としてnameTextField、emailTextField、locationTextFieldを宣言します。だからplsはここでキーボードを閉じる方法を教えてくれます

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    CommonTableViewCell* cell = [[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];//
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    //cell.image = [UIImage imageNamed:@"resources_on.png"];
    cell.textLabel.frame = CGRectMake(TABLE_ROW_HEIGHT + 10, 5, self.view.frame.size.width , 25);

    if (indexPath.section == 0) {
        if (indexPath.row  == 0) {
            cell.textLabel.text = @"Username";

            nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [nameTextField setBackgroundColor:[UIColor clearColor]];
            [nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            nameTextField.placeholder = @"Enter anonymous name";
            [nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];
            [cell addSubview:nameTextField];
            [nameTextField release];

        }else if (indexPath.row == 1) {

            cell.textLabel.text = @"Email"; 
            emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [emailTextField setBackgroundColor:[UIColor clearColor]];
            [emailTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            emailTextField.placeholder = @"example@me.com";
            [emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
            [cell addSubview:emailTextField];
            [emailTextField release];

        }else if (indexPath.row == 2) {

            cell.textLabel.text = @"Location";
            locationTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [locationTextField setBackgroundColor:[UIColor clearColor]];
            [locationTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            locationTextField.placeholder = @"Fetch";
            [cell addSubview:locationTextField];
            [locationTextField release];

        }else if (indexPath.row == 3) {

            cell.textLabel.text = @"Terms of Use";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else if (indexPath.row == 4) {

            cell.textLabel.text = @"Privacy Policy";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else {

        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {

            cell.textLabel.text = @"Terms of Use";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else if (indexPath.row == 1) {

            cell.textLabel.text = @"Privacy Policy";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else {

        }

    }


    return cell;
}

そして、これは私のために働いていません。

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

2 に答える 2

2

最初にGoogleを試してください:search?q = textfield + resignfirstresponder + not + hidden + the +keyboard

スタックオーバーフローについては、この種の質問がたくさんあります。

例:

このフィールドは内部にありUIModalPresentationFormSheetますか?その場合、View Controllerが閉じられるまで、プログラムでキーボードを閉じることができないというのは既知の問題です。

この問題には回避策があります。こちらの質問を確認してください。

差出人:https ://stackoverflow.com/a/6854165/672989 :

于 2013-01-22T09:01:25.807 に答える
1

に代理人を割り当てる必要がありますUITextField

データソース(または他のオブジェクトですが、これは仕事には問題ないようです)は<UITextFieldDelegate>プロトコルに準拠している必要があります。

 ...
 nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
 [nameTextField setBackgroundColor:[UIColor clearColor]];
 [nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
 nameTextField.placeholder = @"Enter anonymous name";
 [nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];

 [nameTextField setDelegate:self]; //add this line

 [cell addSubview:nameTextField];
 //[nameTextField resignFirstResponder]; this doesn't really make sense - creating the text
 //field should not (and does not) bring up the keyboard
 [nameTextField release];
 ....

他のについても同じようにしUITextFieldます。

次に、delegateメソッドが呼び出されるかどうかを確認します(たとえば、NSLogまたはブレークポイントを使用)。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog (@"should return?"); 
    [textField resignFirstResponder]; 
    return YES; 
}

編集

デリゲートを設定する必要があるのは、 UITextFieldごとに1回だけです。通常は、作成時に設定します。

特定のクラスがプロトコルに変換されることをコンパイラに通知するには、ヘッダーファイル<UITextFieldDelegate>のクラス宣言の後に追加します。@interface

たとえば、テーブルのdataSourceとして使用するクラスの名前がMyDataSourceの場合、次のようになります。

@interface MyDataSource: NSObject <UITableViewDelegate,UITextFieldDelegate>

もちろん、特定のプロトコルに正しく準拠するには、必要なすべてのメソッドを実装する必要があります。ドキュメントを確認してください:UITextFieldDelegateプロトコルリファレンス

編集:電子メールの有効性をチェックすることに関して:あなたは有効性をチェックすることができます(これは実際には別の質問です)あなたはこれを行うことができます- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

デリゲートオブジェクトは複数のUITextFieldにデリゲートされているため、編集中のテキストフィールドが電子メールを保持しているものであるかどうかを最初に確認する必要があります。また、テキストフィールドはテーブルセルにあるため、それらすべてへの参照を保持するのは困難です(そして不必要です)。タグを使用して、電子メールのテキストフィールドを他のフィールドから分離できます。44にします。コードを少し変更します。

emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
emailTextField.tag = 44; //add this line

次に、デリゲートメソッドを追加します。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 44)
    {
        //email checking code here
    }
    else
    {
        return YES;
    }
}

メールをチェックする方法については、この回答を見てください:https ://stackoverflow.com/a/7707946/653513

于 2013-01-22T09:24:15.217 に答える