3

Add cell... 行を含む UITableView があり、ユーザーが新しいセルの名前を入力できる「メッセージ」アプリケーションのように、その上にビューを表示してキーボードをポップアップさせたいと考えています。

ユーザー データを取得する方法は他にもいくつかあり、実現しようとしている機能を実装する方法もいくつかあることに気付きました。

たとえば、iPod アプリケーションは、新しいプレイリストを作成する必要があるときにポップアップを表示します。

現在、[セルの追加...] 行が押されたときにファーストレスポンダになるように設定された非表示のテキスト フィールドがあり、入力フィールドと確認ボタンを含むビューをこの非表示フィールドの inputAccessoryView として割り当てます。または、このビューをテーブルのサブビューとして追加し、キーボード通知を使用して配置することもできます。

私がやろうとしていることを達成するためのよりクリーンな方法があるかどうかを知りたいだけです(入力テキストフィールドのinputAccessoryViewを表示するように設定してテキストフィールドのスーパービューにするなど)。いくつかのアプローチを試しましたが、ビューを閉じる必要があるときに、resignFirstResponder を使用してキーボードを閉じることができないようです。inputAccessoryView を非表示にすることはできますが、キーボードは断固として開いたままになり、必要な画面領域を占有します。また、ペン先のビューがファイルの所有者として UIViewController を持つ UIView ではなく、ファイルの所有者として UITableViewController を持つ UITableView である場合、非常に奇妙なエラーが発生します:「テーブルの最初のレスポンダー ビューを設定していますが、わかりませんそのタイプ (セル/ヘッダー/フッター)"

前もって感謝します, ジュリアン・セイペック

4

2 に答える 2

3

UITextView/UITextField クラスのこのメソッドを使用すると、正しい軌道に乗っていsetInputAccessoryViewます。このメソッドを使用すると、必要なビューをキーボードの上部に追加できます。作成したビューは、委譲メソッドを使用して、メインのビュー コントローラーに rejectFirstResponder を指示します。

それで、あなたが始めるために:

@protocol TextFieldAccessoryViewDelegate
@required
- (void)keyboardShouldDismiss;

@end

@interface TextFieldAccessoryView : UIView
@property (nonatomic, retain) id<TextFieldAccessoryViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame withDelegate (id<TextFieldAccessoryViewDelegate>)aDelegate;

@end

実装は次のようになります (ビューを作成するコードのみを投稿します)。

#pragma mark - Private methods
- (void)doneButtonTapped:(id)sender
{
    [delegate keyboardShouldDismiss];
}

- (void)setUpChildrenView
{
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@""];
    [navigationItem setRightBarButtonItem:doneButton];
    [navigationItem setHidesBackButton:YES];

    UINavigationBar *navigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.width, 44.0f)] autorelease];
    [navigationBar pushNavigationItem:navigationItem animated:NO];

    [self addSubview:navigationBar];
}

私は標準的な NavigationBar の外観のビューを使用しましたが、ボタン、テキストフィールド、ロボット ユニコーンの画像、作品など、好きなものを自由に配置できます。

上記のコードで行われていることのすべてを理解できない場合は、委任とプログラムによるビューの作成についてブラッシュアップする必要があるかもしれません。

于 2011-10-10T00:34:38.140 に答える
0
  - (void)viewDidLoad
  {
    [self createAccessoryView];

    [textField setDelegate:self];
    [textField setKeyboardType:UIKeyboardTypeDefault];
    [textField setInputAccessoryView:fieldAccessoryView];


  }


- (void)createAccessoryView
    {

        CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
        fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
        fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
        fieldAccessoryView.tag = 200;

        [fieldAccessoryView setBarStyle:UIBarStyleBlack];

        UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  target:self action:@selector(done:)];

        UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
        [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl setMomentary:YES];
        UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

        [fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
        [segmentButton release];
        [spaceButton release];
        [doneButton release];
        [segmentedControl release];

    }
于 2013-02-19T10:17:27.303 に答える