0

多くのテキストフィールドを使用するアプリを作成しています。それらのほとんどは静的なtableViews内にあります。分割ビューアプリケーションテンプレートを使用します。左側のパネルから選択したすべてのカテゴリは、右側のパネルの2番目のビュー内にストーリーボードシーンを表示します。「完了」ボタンを使用してキーボードを削除したいのですが、単純なビューで機能するように試したものはすべて、このような状況では機能しません。これを手伝ってくれませんか。

ps提示されたストーリーボードシーンの実装ファイル内のキーボードを閉じようとします。分割ビューコントローラーの詳細シーン内で何かを行う必要がありますか?

これが私のシーンのコードです:

.h
    #import <UIKit/UIKit.h>
    @interface AfoEsoda : UITableViewController <UITextFieldDelegate>{
    }
    @property (strong, nonatomic) IBOutlet UITextField *merismataTF;
    -(IBAction)hideKeyboard:(id)sender;
    @end

.m
@synthesize merismataTF;

        - (void)viewDidLoad
        {
            [super viewDidLoad];
            merismataTF.delegate=self ;
        }

//---------Hide Keyboard-------------------
//Tried but didn't work
-(IBAction)hideKeyboard:(id)sender {
    [merismataTF resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
//Of course i do not use both methods at the same time.

編集:テキストフィールドのデリゲートを自分自身に設定すると、このクラッシュが発生します: textfieldShouldReturn Crach

4

1 に答える 1

1

textFieldのデリゲートを実装し、デリゲートをselfに設定し、デリゲートのメソッドで試してください

 - (BOOL)textFieldShouldReturn:(UITextField *)textField

セットする

[textField resignFirstResponder];

別の方法は、ビューのすべてのサブビューを通過することであり、それがテキストフィールドである場合は、ファーストレスポンダーを辞任します。

for(int i=0;i<self.view.subviews.count;i++)
{
if([[self.view.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
{
    if([[self.view.subviews objectAtIndex:i] isFirstResponder])
         [[self.view.subviews objectAtIndex:i] resignFirstResponder];
}}

はい、分かりました。これをtextFieldShouldReturnメソッドとともに使用します。これがあなたの答えです。テキストフィールドをプロパティとして宣言し、allocを使用して、セルごとに何度も初期化します。おそらく、最後の行でのみ正しく機能します。

次に、cellForRowメソッドがどのように表示されるかの例を示します。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *cellIdentifier = @"My cell identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *newTextField;
if(cell == nil)
 {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  newTextField = [[UITextField alloc] initWithFrame:CGRectMake:(0,0,25,25)];
  newTextField.tag = 1;
  newTextField.delegate = self;
  [cell.contentView addSubview:newTextField];
  }
  else
     newTextField = (UITextField *)[cell.contentView viewWithTag:1];

次に、特定の行にtextFieldの値が必要な場合は、次を使用します。

UITextField *someTextField = (UITextField *)[[tableView cellForRowAtIndexPath:indexPath].contentView viewWithTag:1];
NSLog(@"textField.text = %@", someTextField.text);
于 2012-08-27T13:49:11.437 に答える