2011-07-22 6 views
3

私はUITableViewにAdd cell ...という行があり、 "Messages"アプリケーションのように上のビューでキーボードをポップアップしたいと思っています新しいセルの名前を入力します。UITextFieldのinputAccessoryViewをそのスーパービューに設定する

ユーザーデータを取得するにはいくつかの方法があり、達成しようとしている機能を実装するいくつかの方法があることを認識しています。

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

今、私は、セルの追加...行が押されたときの最初のレスポンダになるように隠されたテキストフィールドを持っています。そして、このフィールドのinputAccessoryViewとして入力フィールドと確認ボタンを含むビューを割り当てますフィールド。また、このビューをテーブルのサブビューとして追加し、キーボード通知を使用して配置することもできます。

入力したtextFieldのinputAccessoryViewをtextFieldのスーパービューとして表示するなど、私がやろうとしていることを達成するためのクリーンな方法があるかどうかを知りたいだけです。私はいくつかのアプローチを試みましたが、ビューを閉じるべきときにresignFirstResponderを使ってキーボードを閉じることができないようです。私はinputAccessoryViewが消えるようにすることができますが、キーボードは必然的に開いていて、必要なスクリーンの不動産を占めています。また、ペンの見方は、ファイルの所有者としてUIViewControllerを持つUIViewではなくFile's OwnerとしてUITableViewControllerを持つUITableViewである場合、非常に奇妙なエラーが発生します。「テーブルの最初のレスポンダビューを設定しますが、わかりませんそのタイプ(セル/ヘッダ/フッタ)」事前に

おかげで、 ジュリアンCeipek

答えて

2

あなたはUITextView/UITextFieldのクラスにsetInputAccessoryViewで正しい軌道に乗っているこの方法では、あなたは、任意のビューあなたを追加することができますキーボードの上部に欲しい。 作成したビューは、委譲メソッドを使用してメインビューコントローラにresignFirstResponderを通知します。あなたが始めるためにそう

、:

@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を使用していましたあなたは好きなものを置くことができ、ボタン、テキストフィールド、ロボットのユニコーンの画像、作品を含めることができます

上記のようなことが起こっていなければあなたはDelegationをブラッシュアップし、プログラムでビューを作成する必要があります。

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]; 

    } 
関連する問題