6

私は動的な数を持ってUITextFieldsいますが、そのすべてにinputAccessoryView. これはUIToolbar、ボタンが付いた で構成されます。このボタンが押されると、メソッドが呼び出されますが、このメソッド内で、基になる にアクセスする機能が必要UITextFieldです。

誰かがこれがどのように可能かアドバイスしてもらえますか?

// Create the toolbar controls
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(navigationBarDoneButtonPressed:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

// Setup the toolbar
navigationToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
[navigationToolbar setItems:@[flexibleSpace, doneButton]];

// In a UITableView I create a number of cells which contain UITextFields....

// This is the method that gets called when a user presses the done button
- (void)navigationBarDoneButtonPressed:(id)sender
{
    NSLog(@"Based on sender I need to access the underlying UITextField.");
}
4

2 に答える 2

7

現在フォーカスされている変数を追跡し、UITextFieldその変数をinputAccesoryViewメソッドで使用できます。

.h ファイル内:UITextFieldDelegateプロトコルに準拠していることを確認してください:

@interface MyViewController : UIViewController <UITextFieldDelegate>

このプロパティを追加します。

@property (assign, nonatomic) UITextField *activeTextField;

.m ファイル内: 動的テキストフィールドの作成中:

...
theTextField.delegate=self;
...

そして、これらのUITextFieldDelegateプロトコルの実装を追加します。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeTextField = nil;
}

そして今、あなたによって呼び出されたメソッドでinputAccesoryView

- (void)navigationBarDoneButtonPressed:(id)sender{//Just an example
    self.activeTextField.text=@"Test";
}
于 2013-08-23T16:00:02.083 に答える