2012-01-21 12 views
9

私の最初のiPhoneアプリを探す際に、iOSキーボードのリターンキーを処理する正しい方法について投稿しました。今度は、キーボードの上のツールバーをprev/nextとdoneボタンで把握する必要があります。私は次のサイトの例を使って作業しています: Input Accessory Viewいくつかのテキストフィールドにアクセサリビューを入力するためにUIToolbarを追加する

この例は機能しますが、ボタン付きの通常のビューの代わりにUIToolbarとUIBarButtonItemを使用したいと思います。私はいろいろな組み合わせを試してきましたが、まだ動作しませんここまで私がこれまで持っていたことがあります。

ヘッダー:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITextFieldDelegate> 
{ 
    UITextField* txtActiveField; 
    UIToolbar* keyboardToolbar; 
    UIBarButtonItem* btnDone; 
    UIBarButtonItem* btnNext; 
    UIBarButtonItem* btnPrev; 
} 

@property (nonatomic, strong) IBOutlet UITextField* firstNameTextField; 
@property (nonatomic, strong) IBOutlet UITextField* lastNameTextField; 
@property (nonatomic, strong) IBOutlet UITextField* cityTextField; 

@property (nonatomic, strong) UITextField* txtActiveField; 
@property (nonatomic, strong) UIToolbar* keyboardToolbar; 
@property (nonatomic, strong) UIBarButtonItem* btnDone; 
@property (nonatomic, strong) UIBarButtonItem* btnNext; 
@property (nonatomic, strong) UIBarButtonItem* btnPrev; 

-(void)createInputAccessoryView; 

@end 

出典:

#import "ViewController.h" 

@implementation ViewController 

@synthesize firstNameTextField = _firstNameTextField; 
@synthesize lastNameTextField = _lastNameTextField; 
@synthesize cityTextField = _cityTextField; 

@synthesize txtActiveField = _txtActiveField; 
@synthesize keyboardToolbar = _keyboardToolbar; 
@synthesize btnDone = _btnDone; 
@synthesize btnNext = _btnNext; 
@synthesize btnPrev = _btnPrev; 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self createInputAccessoryView]; 
} 

- (void)viewDidUnload 
{ 
    [self setFirstNameTextField:nil]; 
    [self setLastNameTextField:nil]; 
    [self setCityTextField:nil]; 

    [self setTxtActiveField:nil]; 
    [self setKeyboardToolbar:nil]; 
    [self setBtnDone:nil]; 
    [self setBtnNext:nil]; 
    [self setBtnPrev:nil]; 

    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

-(void)gotoPrevTextfield 
{ 
    if (self.txtActiveField == self.firstNameTextField) 
    { 
     return; 
    } 
    else if (self.txtActiveField == self.lastNameTextField) 
    { 
     [self.firstNameTextField becomeFirstResponder]; 
    } 
    else if (self.txtActiveField == self.cityTextField) 
    { 
     [self.lastNameTextField becomeFirstResponder]; 
    }   
} 

-(void)gotoNextTextfield 
{ 
    if (self.txtActiveField == self.firstNameTextField) 
    { 
     [self.lastNameTextField becomeFirstResponder]; 
    } 
    else if (self.txtActiveField == self.lastNameTextField) 
    { 
     [self.cityTextField becomeFirstResponder]; 
    } 
    else if (self.txtActiveField == self.cityTextField) 
    { 
     return; 
    }   
} 

-(void)doneTyping 
{ 
    [self.txtActiveField resignFirstResponder]; 
} 

-(void)createInputAccessoryView 
{ 
    self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; 
    self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; 
    self.keyboardToolbar.tintColor = [UIColor darkGrayColor]; 

    btnPrev = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(gotoPrevTextfield:)]; 
    btnNext = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(gotoNextTextfield:)]; 
    btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping:)]; 

    [self.keyboardToolbar addSubview:(UIView*)btnPrev]; 
    [self.keyboardToolbar addSubview:(UIView*)btnNext]; 
    [self.keyboardToolbar addSubview:(UIView*)btnDone]; 

    [self.firstNameTextField setInputAccessoryView:self.keyboardToolbar]; 
    [self.lastNameTextField setInputAccessoryView:self.keyboardToolbar]; 
    [self.cityTextField setInputAccessoryView:self.keyboardToolbar]; 
} 

-(void)textFieldDidBeginEditing:(UITextField*)textField 
{ 
    self.txtActiveField = textField; 
} 
@end 

私はアプリケーションを起動すると、私は得る:

012-01-21 09:31:19.798 KeyboardToolbar[14772:f803] -[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6b19350 
2012-01-21 09:31:19.799 KeyboardToolbar[14772:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6b19350' 
*** First throw call stack: 
(0x13bc052 0x154dd0a 0x13bdced 0x1322f00 0x1322ce2 0x5042f 0x4a72b 0x3422 0x2a58 0xd964e 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x2728 0x2685 0x1) 
terminate called throwing an exception 

私の仮定は私が正しく要素の一つに割り当てられていないということです。また、ボタンのクラスにフィールドを入れる必要があるのか​​、それともツールバーにフィールドを追加するだけでよいのか不思議です。私は、ファイルの先頭にすべてのメソッドを置く必要がないように、ヘッダに行を挿入します。あなたがCでできるような方法を宣言する構文をまだ知らないでください。

ありがとう。

更新:

だから私はcreateInputAccessoryViewにいくつかの変更を行いました。私は今ボタンを追加するaddItemsを使用しています。私はボタンの変数をローカルにしました(ただし、前と同じ問題がありました)。良いニュースは、アプリがもうクラッシュしないということです、悪いニュースは、ツールバーが表示されますが、ボタンがないことです。ボタンを実際に表示させるために何が必要なのかわからない。私が見たすべての例は似ています。

-(void)createInputAccessoryView 
{ 
    self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; 
    self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; 
    self.keyboardToolbar.tintColor = [UIColor darkGrayColor]; 

    UIBarButtonItem* previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPrevTextfield)]; 
    UIBarButtonItem* nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoNextTextfield)]; 
    UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping:)]; 

    [keyboardToolbar setItems:[NSArray arrayWithObjects: previousButton, nextButton, flexSpace, doneButton, nil] animated:NO]; 

    [self.firstNameTextField setInputAccessoryView:self.keyboardToolbar]; 
    [self.lastNameTextField setInputAccessoryView:self.keyboardToolbar]; 
    [self.cityTextField setInputAccessoryView:self.keyboardToolbar]; 
} 

答えて

8

UIBarButtonItemUIViewサブクラスではありませんので、あなたはUIToolbarのサブビューとして追加することはできません。ツールバーの– setItems:animated:メソッドを使用して、これらのボタンを追加してください。

+0

を使用してみてください、私はあなたが提案した変更を加えました。それはクラッシュを解決しましたが、ボタンは表示されませんでした(ツールバーには少なくとも表示されます)。私はボタンを追加する方法を更新しましたが、それはいずれも役に立たなかった。他のアイデア?ありがとう。 –

+1

あなたは項目を設定する行に 'keyboardToolbar'の前に' self'を追加する必要はありませんか?それはおそらく問題ではありませんが、おそらく問題はありません。また、ボタンが誤って離されていないことを確認してください。 ARCを使用していますか? –

+0

それは私が本当に理解できないものです。ありがとうbtw、私は自己を追加しました。項目を追加すると、ボタンが表示されます。自分自身を使わずに変数を使うだけなら、あなたは何にアクセスしていますか? –

-2

問題は、このサブ付属品の表示を追加する場所になければなりません。

0

私の現在のプロジェクトでは、UITableView内のテキストフィールドによって開始されるUIDatePickerの上にツールバーを追加するだけで、コードを使用することができました。私は静的な細胞を使用しています。

私のために働く。

私はIBOutletsとしてUITextFieldsを持っているだけInputAccessoryView

[textFieldName setInputAccessoryView:self.keyboardToolbar]; 
[textFieldEmail setInputAccessoryView:self.keyboardToolbar]; 
[textFieldPhone setInputAccessoryView:self.keyboardToolbar]; 
[textFieldDate setInputAccessoryView:self.keyboardToolbar]; 
[textFieldSize setInputAccessoryView:self.keyboardToolbar]; 

さて、次と前のボタンが魅力のように動作する(だけのテーブルダウン途中で)設定するには、このコードを使用します。私はまだバックアップするときにテーブルを一番上に手動でスクロールしなければならなかった。何らかの理由でUITableViewControllerがスクロールダウンのシナリオを処理するだけですが、これは非常に奇妙ですが、これは以前のコードスニペットです。

- (void) gotoPrev { 
if (self.activeField == textFieldName) { // name --> nowhere 
    return; 
} 
else if (self.activeField == textFieldEmail) { // email --> name 
    [textFieldName becomeFirstResponder]; 
} 
else if (self.activeField == textFieldPhone) { // phone --> email 
    [textFieldEmail becomeFirstResponder]; 
    // Scroll jeegar 
    NSIndexPath * myIndexPath= [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView scrollToRowAtIndexPath:myIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 
else if (self.activeField == textFieldDate) { // date --> phone 
    [textFieldPhone becomeFirstResponder]; 
} 
else if (self.activeField == textFieldSize) { // people --> date 
    [textFieldDate becomeFirstResponder]; 
}  
} 
1

これが遅すぎるかどうかはわかりません。UIToolbarをUIKeyboardに追加する方法について解説しました。キーボードの種類はUITextFieldの内容によって変更され、Githubには "Previous"、 "Next"、 "Done"のUIBarButtonItemが処理されます。そして、 TextFieldsがtableviewの異なるセクションにある場合、それはより難しいものになります。

+0

Githubリンクが壊れています – Besi

+0

リンクが壊れていません。 – mrd

0

私はあなたのボタンがあるため、次のコードだけ表示されていないと思う:

self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; 
self.keyboardToolbar.tintColor = [UIColor darkGrayColor]; 

私は前にそのコードを試してみましたが、念のためにしていた代わりに

self.keyboardToolbar.barStyle = UIBarStyleBlack; 
self.keyboardToolbar.tintColor = [UIColor whiteColor]; 

関連する問題