2

iOS キーボードの上に [前へ]、[次へ]、[完了] ツールバーを追加し、これらのアクションを処理する 2 つのメソッドがあります。これらのメソッドを一度コーディングして、複数の UITableViewControllers で再利用する方法を探しています。(ドライコード)

これらのメソッドをコピーして各 UITableViewController に貼り付けています。小さな変更を加えると、その変更をコピーしてどこにでも貼り付ける必要があります。以下のコードは単なる例です。コード内で何度も繰り返しているようです。

再利用したいコードの例を次に示します。

- (void) createInputAccessoryView
{
    _inputAccView = [[UIView alloc] initWithFrame:CGRectMake(10,0,310,42)];

    UIToolbar *keyboardToolbar = [[UIToolbar alloc] init];
    keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
    [keyboardToolbar sizeToFit];

    _segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
    [_segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [_segmentedControl addTarget:self action:@selector(nextPrevious:) forControlEvents:UIControlEventValueChanged];

    UIBarButtonItem *nextPrevButton = [[UIBarButtonItem alloc] initWithCustomView:_segmentedControl];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];

    NSArray *barItems = [NSArray arrayWithObjects:nextPrevButton, flexSpace, doneBtn, nil];
    [keyboardToolbar setItems:barItems];

    [_inputAccView addSubview:keyboardToolbar];

}

- (void) nextPrevious:(id) sender
{
    switch(_activeTxtField.tag) {
        case 1:
            //Recipe Name
            if (_segmentedControl.selectedSegmentIndex == 1){
                [_descriptionTextField becomeFirstResponder];
                _activeTxtField = _descriptionTextField;
            }
            break;
        case 2:
            //Recipe Description
            if (_segmentedControl.selectedSegmentIndex == 0){
                [_nameTextField becomeFirstResponder];
                _activeTxtField = _nameTextField;
            }
        default:
            break;
    }
}
4

1 に答える 1

3

UIView共通入力アクセサリ ビューを定義するカスタムを作成します。デリゲートの定義を含めて、アクセサリ ビューを使用するクラスが適切に前/次のボタン タップなどを処理できるようにする必要があります。キーボード アクセサリ ビューのヘッダー ファイルの例を次に示します。

#import <UIKit/UIKit.h>

@class KeyboardAccessoryView;

@protocol KeyboardAccessoryViewDelegate <NSObject>

-(void)accessoryNext:(id)sender;
-(void)accessoryPrevious:(id)sender;

@end

@interface InputAccessoryView : UIView

@property (nonatomic, weak) id<KeyboardAccessoryViewDelegate> delegate;
@property (nonatomic, setter = enablePrevious:) BOOL previousEnabled;
@property (nonatomic, setter = enableNext:) BOOL nextEnabled;

-(id)initPreviousNextAccessory;

@end

編集 - での使用の詳細を表示しますUIViewController

.h ファイル:

#import <UIKit/UIKit.h>
#import "KeyboardAccessoryView.h"

@interface MyViewController : UIViewController <KeyboardAccessoryViewDelegate>

//...

@end

.m ファイル:

#import "MyViewController.h"

@interface MyViewController () {
    KeyboardAccessoryView *inputAccessoryView;
}

@end

@implementation MyViewController

//...

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    inputAccessoryView = [[KeyboardAccessoryView alloc] initPreviousNextAccessory];
    inputAccessoryView.delegate = self;

    //...
}


-(void)accessoryNext:(id)sender{
    // handle Next
}

-(void)accessoryPrevious:(id)sender{
    // handle Previous
}

//...

@end
于 2012-12-29T13:58:57.213 に答える