2

我有 2 种方法可以在 iOS 键盘上方添加上一个、下一个和完成的工具栏并处理这些操作。我正在寻找一种方法来对这些方法进行一次编码并在多个 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 回答