如何在iOS中重用UIView和方法 - DRY代码

时间:2012-12-29 13:43:52

标签: objective-c ios design-patterns dry code-reuse

我有两种方法可以添加Previous,Next&在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;
    }
}

1 个答案:

答案 0 :(得分: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