[iOS]智能搜索列表分离View和Controller

xiaoxiao2021-07-04  226

demo:https://download.csdn.net/download/u012881779/10711030 分离View和Controller,将视图 UI 和业务逻辑分开。 ViewController

#import "ViewController.h" #import "GATopView.h" #import "GABottomView.h" @interface ViewController () @property (strong, nonatomic) GATopView *topView; @property (strong, nonatomic) GABottomView *bottomView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 顶部 _topView = [[[NSBundle mainBundle] loadNibNamed:@"GATopView" owner:self options:nil] firstObject]; // 涉及到处理逻辑,这里要先添加视图再进行初始化操作 [self.view addSubview:_topView]; [_topView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 74.0)]; [_topView initializeAction]; [_topView.searchText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; // 底部 _bottomView = [[GABottomView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-50, [UIScreen mainScreen].bounds.size.width, 50)]; [_bottomView addTarget:self action:@selector(buttonOnclick:) section:0]; [self.view addSubview:_bottomView]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } /** 固定的方法名,在控制器中无法改变 事件区分 2 返回按钮 5 搜索按钮 10 搜索图标 20 选择图片按钮 */ - (void)tapViewFuncation:(UIView *)sender { UIView *tempView = sender; NSInteger mark = [[tempView getViewMark] integerValue]; if (mark == 2) { [self tapReturnButtonAction]; } else if (mark == 5) { [self tapSearchButtonAction]; } else if (mark == 10) { } else if (mark == 20) { [self tapCameraAction]; } } /** 可变的方法名,可在初始化后设置回调的方法(addTarget: action:@selector(buttonOnclick:) section:) 底部事件区分 12 imageView 22 收藏 */ - (void)buttonOnclick:(id)sender { UIView *tempView = sender; NSInteger mark = [tempView getViewTypeOfTag]; if (mark == 12) { } else if (mark == 22) { } } // 点击返回按钮 - (void)tapReturnButtonAction { } // 点击搜索按钮 - (void)tapSearchButtonAction { [self.view endEditing:YES]; } // 点击选择相片 - (void)tapCameraAction { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:cameraAction]; UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:albumAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil]; } // 智能搜索 - (void)textFieldDidChange:(UITextField *)textField { // 防止输入拼音状态时查询 // if (textField.markedTextRange == nil) { NSLog(@"text:%@", textField.text); NSString *text = textField.text; if (![text isEqualToString:@""]) { } else { } // } } @end

GATopView

#import <UIKit/UIKit.h> #import "UIView+ExtraProperty.h" @interface GATopView : UIView @property (weak, nonatomic) IBOutlet UIButton *returnBut; @property (weak, nonatomic) IBOutlet UIButton *searchBut; @property (weak, nonatomic) IBOutlet UITextField *searchText; @property (strong, nonatomic) UIImageView *leftImageView; @property (strong, nonatomic) UIButton *rightButton; - (void)initializeAction; @end #import "GATopView.h" @implementation GATopView - (void)initializeAction { [_returnBut setViewMarkWith:@"2"]; [self addEvent:_returnBut]; [_searchBut setViewMarkWith:@"5"]; [self addEvent:_searchBut]; self.searchText.clearButtonMode = UITextFieldViewModeWhileEditing; _leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_menu_search"]]; _leftImageView.contentMode = UIViewContentModeScaleAspectFit; _leftImageView.frame = CGRectMake(0, 0, 30, 20); [_leftImageView setViewMarkWith:@"10"]; [self addEvent:_leftImageView]; self.searchText.leftView = _leftImageView; self.searchText.leftViewMode = UITextFieldViewModeAlways; _rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; _rightButton.frame = CGRectMake(0, 0, 30, 20); [_rightButton setImage:[UIImage imageNamed:@"ic-ss-xj"] forState:UIControlStateNormal]; [_rightButton setViewMarkWith:@"20"]; [self addEvent:_rightButton]; self.searchText.rightView = _rightButton; self.searchText.rightViewMode = UITextFieldViewModeAlways; } @end

GABottomView

#import <UIKit/UIKit.h> #import "UIView+Action.h" #import "UIView+ExtraTag.h" @interface GABottomView : UIView @property (strong, nonatomic) UIImageView *leftImageView; @property (strong, nonatomic) UIButton *rightButton; @end #import "GABottomView.h" @implementation GABottomView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor lightGrayColor]; _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 80, self.frame.size.height)]; _leftImageView.backgroundColor = [UIColor orangeColor]; _leftImageView.userInteractionEnabled = YES; [self addSubview:_leftImageView]; [_leftImageView setViewTypeForTag:12]; [self addEventIn:_leftImageView]; _rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; _rightButton.frame = CGRectMake(150, 0, 80, self.frame.size.height); _rightButton.backgroundColor = [UIColor blueColor]; [_rightButton setTitle:@"收藏" forState:UIControlStateNormal]; [self addSubview:_rightButton]; [_rightButton setViewTypeForTag:22]; [self addEventIn:_rightButton]; } return self; } @end

为UIView关联属性 方式一 UIView+ExtraProperty

#import <UIKit/UIKit.h> @interface UIView (ExtraProperty) /// 为view关联自定义的标签 - (void)setViewMarkWith:(id)mark; /// 从view获取关联的标签 - (id)getViewMark; /// 移除关联对象 - (void)removeAssociatedObjects; /// 获取view的父控制器 - (UIViewController *)supViewController; /// 添加事件 - (void)addEvent:(UIView *)view; @end #import "UIView+ExtraProperty.h" #import <objc/runtime.h> static void * KEY_VIEW_MARK = &KEY_VIEW_MARK; @implementation UIView (ExtraProperty) /** 方法: objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key, id _Nullable value, objc_AssociationPolicy policy) 参数: object 关联者 key key与value是一一对应的关系,必须确保key全局唯一。 value 关联的对象 policy 关联策略: typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) { OBJC_ASSOCIATION_ASSIGN = 0, 相当@property(assign) OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, 相当@property(strong, nonatomic) OBJC_ASSOCIATION_COPY_NONATOMIC = 3, 相当@property(copy, nonatomic) OBJC_ASSOCIATION_RETAIN = 01401, 相当@property(strong, atomic) OBJC_ASSOCIATION_COPY = 01403 相当@property(copy, atomic) }; */ - (void)setViewMarkWith:(id)mark { objc_setAssociatedObject(self, KEY_VIEW_MARK, mark, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } /** 方法: objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key) */ - (id)getViewMark { return objc_getAssociatedObject(self, KEY_VIEW_MARK); } /** 方法:用来移除关联对象 objc_removeAssociatedObjects(id _Nonnull object) */ - (void)removeAssociatedObjects { objc_removeAssociatedObjects(self); // 或者 // [self setViewMarkWith:nil]; } - (UIViewController *)supViewController { for (UIView* next = [self superview]; next; next = next.superview) { UIResponder *nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) { return (UIViewController *)nextResponder; } } return nil; } - (void)addEvent:(UIView *)view { UIViewController *supVC = [self supViewController]; if ([view isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton*)view; [button addTarget:supVC action:@selector (tapViewFuncation:) forControlEvents:UIControlEventTouchUpInside]; } else if ([view isKindOfClass:[UIView class]]) { view.userInteractionEnabled = YES; UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:supVC action:@selector (tapImageViewFuncation:)]; [view.gestureRecognizers enumerateObjectsUsingBlock:^(__kindof UIGestureRecognizer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [view removeGestureRecognizer:obj]; }]; [view addGestureRecognizer:gesture]; } } @end

UIViewController+ExtraProperty

#import <UIKit/UIKit.h> @interface UIViewController (ExtraProperty) @end #import "UIViewController+ExtraProperty.h" @implementation UIViewController (ExtraProperty) - (void)tapImageViewFuncation:(UITapGestureRecognizer *)sender { [self tapViewFuncation:sender.view]; } - (void)tapViewFuncation:(UIView *)sender { } @end

为UIView关联属性 方式二 UIView+Action

#import <UIKit/UIKit.h> @interface UIView(Action) - (SEL __nullable)itemAction; - (id __nullable)itemTarget; - (NSInteger)itemPosition; - (NSInteger)itemSection; - (void)addTarget:(id _Nonnull)target action:(SEL _Nonnull)action section:(NSInteger)section; - (void)addTarget:(id _Nonnull)target action:(SEL _Nonnull)action position:(NSInteger)position section:(NSInteger)section; - (void)addEventIn:(UIView * _Nonnull)view; @end #import "UIView+Action.h" #import "UIView+ExtraTag.h" #import <objc/runtime.h> static void * KEY_ITEM_TARGET = &KEY_ITEM_TARGET; static void * KEY_ITEM_ACTION = &KEY_ITEM_ACTION; static void * KEY_ITEM_POSITION = &KEY_ITEM_POSITION; static void * KEY_ITEM_SECTION = &KEY_ITEM_SECTION; @implementation UIView(Action) - (SEL)itemAction { return NSSelectorFromString(objc_getAssociatedObject(self, KEY_ITEM_ACTION)); } - (id)itemTarget { return objc_getAssociatedObject(self, KEY_ITEM_TARGET); } - (NSInteger)itemPosition { return [objc_getAssociatedObject(self, KEY_ITEM_POSITION) integerValue]; } - (NSInteger)itemSection { return [objc_getAssociatedObject(self, KEY_ITEM_SECTION) integerValue]; } - (void)addTarget:(id _Nonnull)target action:(SEL _Nonnull)action section:(NSInteger)section { [self addTarget:target action:action position:0 section:section]; } - (void)addTarget:(id _Nonnull)target action:(SEL _Nonnull)action position:(NSInteger)position section:(NSInteger)section { objc_setAssociatedObject(self, KEY_ITEM_TARGET, target, OBJC_ASSOCIATION_ASSIGN); objc_setAssociatedObject(self, KEY_ITEM_ACTION, NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC); objc_setAssociatedObject(self, KEY_ITEM_POSITION, @(position), OBJC_ASSOCIATION_RETAIN_NONATOMIC); objc_setAssociatedObject(self, KEY_ITEM_SECTION, @(section), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)onButtonClicked:(UIView *)sender { [self callTarget:sender]; } - (void)textFieldDidEndEditing:(UITextField *)textField { [self callTarget:textField]; } - (void)onTap:(UIGestureRecognizer *)gesture { [self callTarget:gesture.view]; } - (void)callTarget:(UIView *)sender { if (![self.itemTarget respondsToSelector:self.itemAction]) { return; } [sender setPositionForTag:self.itemPosition]; [sender setSectionForTag:self.itemSection]; IMP imp = [self.itemTarget methodForSelector:self.itemAction]; void (*function) (id, SEL, UIView *) = (void *)imp; function (self.itemTarget, self.itemAction, sender); } - (void)addEventIn:(UIView *)view { if ([view isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton*)view; [button addTarget:self action:@selector (onButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; } else if ([view isKindOfClass:[UITextField class]]) { UITextField *textFiled = (UITextField *)view; [textFiled addTarget:self action:@selector (textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd]; } else if ([view isKindOfClass:[UIView class]]) { view.userInteractionEnabled = YES; UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector (onTap:)]; [view.gestureRecognizers enumerateObjectsUsingBlock:^(__kindof UIGestureRecognizer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [view removeGestureRecognizer:obj]; }]; [view addGestureRecognizer:gesture]; } } @end

UIView+ExtraTag

#import <UIKit/UIKit.h> @interface UIView (ExtraTag) /** * viewType为view的类别,例如商品、店铺等 */ - (void)setViewTypeForTag:(NSInteger)viewType; /** * position一般为view在列表中的row number */ - (void)setPositionForTag:(NSInteger)position; /** * section一般为view在列表中section number */ - (void)setSectionForTag:(NSInteger)section; /** * extraInfo一般用view在嵌套列表中的row number */ - (void)setExtraInfoForTag:(NSInteger)extraInfo; - (NSInteger)getViewTypeOfTag; - (NSInteger)getPositionOfTag; - (NSInteger)getSectionOfTag; - (NSInteger)getExtraInfoOfTag; @end #import "UIView+ExtraTag.h" #import <objc/runtime.h> static void * KEY_VIEW_TYPE = &KEY_VIEW_TYPE; static void * KEY_POSITION = &KEY_POSITION; static void * KEY_SECTION = &KEY_SECTION; static void * KEY_EXTRA_INFO = &KEY_EXTRA_INFO; @implementation UIView (ExtraTag) - (void)setViewTypeForTag:(NSInteger)viewType { objc_setAssociatedObject(self, KEY_VIEW_TYPE, [NSNumber numberWithInteger:viewType], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)setPositionForTag:(NSInteger)position { objc_setAssociatedObject(self, KEY_POSITION, [NSNumber numberWithInteger:position], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)setSectionForTag:(NSInteger)section { objc_setAssociatedObject(self, KEY_SECTION, [NSNumber numberWithInteger:section], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)setExtraInfoForTag:(NSInteger)extraInfo { objc_setAssociatedObject(self, KEY_EXTRA_INFO, [NSNumber numberWithInteger:extraInfo], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (NSInteger)getViewTypeOfTag { return [objc_getAssociatedObject(self, KEY_VIEW_TYPE) integerValue]; } - (NSInteger)getPositionOfTag { return [objc_getAssociatedObject(self, KEY_POSITION) integerValue]; } - (NSInteger)getSectionOfTag { return [objc_getAssociatedObject(self, KEY_SECTION) integerValue]; } - (NSInteger)getExtraInfoOfTag { return [objc_getAssociatedObject(self, KEY_EXTRA_INFO) integerValue]; } @end

示意图:

转载请注明原文地址: https://www.6miu.com/read-4821272.html

最新回复(0)