ICRQInputPopup.m 10 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
//
//  ICRQInputPopup.m
//  XFFruit
//
//  Created by Xummer on 15/6/8.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRQInputPopup.h"

#define BTN_HEIGHT          (44)

@interface ICRQInputPopup () <UITextFieldDelegate>
@property (strong, nonatomic) IBTUIView *m_topView;
@property (strong, nonatomic) IBTUIButton *m_closeBtn;
@property (strong, nonatomic) IBTUIButton *m_bottomBtn;
@property (strong, nonatomic) IBTTableViewInfo *m_tableViewInfo;
@end

@implementation ICRQInputPopup

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    
    [self addKeyboardNotification];
    
    self.m_bEnableTapToDismiss = NO;
    self.m_bShowShowInCenter = YES;
    
    [self initTableViewInfo];
    
    return self;
}

- (void)initTableViewInfo {
    
    self.m_topView = [[IBTUIView alloc] initWithFrame:(CGRect){
        .origin.x = 0,
        .origin.y = 0,
        .size.width = self.width,
        .size.height = BTN_HEIGHT
    }];
    
    self.m_topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    [self addSubview:_m_topView];
    
    IBTUILabel *titleLabel = [[IBTUILabel alloc] initWithFrame:_m_topView.bounds];
    titleLabel.text = @"填写内容";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor grayColor];
    [titleLabel autoresizingWithStrechFullSize];
    [_m_topView addSubview:titleLabel];
    
    self.m_closeBtn = [IBTUIButton buttonWithType:UIButtonTypeCustom];
    [_m_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [_m_closeBtn setTitleColor:ICR_TINTCOLOR forState:UIControlStateNormal];
    _m_closeBtn.frame = (CGRect){
        .origin.x = _m_topView.width - 90,
        .origin.y = 0,
        .size.width = 90,
        .size.height = _m_topView.height
    };
    [_m_closeBtn addTarget:self action:@selector(dismissPopup)
          forControlEvents:UIControlEventTouchUpInside];
    _m_closeBtn.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_m_topView addSubview:_m_closeBtn];
    
    self.m_bottomBtn = [IBTUIButton buttonWithType:UIButtonTypeCustom];
    [_m_bottomBtn setTitle:@"保存" forState:UIControlStateNormal];
    [_m_bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_m_bottomBtn setBackgroundImage:[UIImage imageWithColor:ICR_ORANGE_BTN_COLOR]      forState:UIControlStateNormal];
    _m_bottomBtn.frame = (CGRect){
        .origin.x = 0,
        .origin.y = self.height - BTN_HEIGHT,
        .size.width = self.width,
        .size.height = BTN_HEIGHT
    };
    [_m_bottomBtn addTarget:self action:@selector(onBottomBtnAction:)
           forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_m_bottomBtn];
    
    self.m_tableViewInfo =
    [[IBTTableViewInfo alloc] initWithFrame:(CGRect){
        .origin.x = 0,
        .origin.y = _m_topView.bottom,
        .size.width = self.width,
        .size.height = self.height - _m_bottomBtn.height - _m_topView.height
    } style:UITableViewStylePlain];
    
    IBTTableView *tableV = [_m_tableViewInfo getTableView];
    [self addSubview:tableV];
}

- (void)updateSuervyData:(NSArray *)arrDatas {
    [_m_tableViewInfo removeSectionAt:0];
    
    IBTTableViewSectionInfo *secInfo = [IBTTableViewSectionInfo sectionInfoDefaut];
    
    IBTTableViewCellInfo *cellInfo;
    
    for (NSString *nsTitle in arrDatas) {
        cellInfo = [IBTTableViewCellInfo editorCellForSel:nil
                                                   target:nil
                                                    title:[nsTitle stringByAppendingString:@":"]
                                                   margin:0
                                                      tip:@"请输入"
                                              autoCorrect:NO
                                                    focus:NO
                                                     text:nil];
        
        [cellInfo addUserInfoValue:@( NSTextAlignmentRight )
                            forKey:CInfoEditorAlignKey];
        
        [cellInfo addUserInfoValue:@( UIKeyboardTypeDefault )
                            forKey:CInfoEditorKeyboardTypeKey];
        
        [secInfo addCell:cellInfo];
    }
    
    cellInfo = [IBTTableViewCellInfo editorCellForSel:nil
                                              target:nil
                                               title:@"零售价:"
                                              margin:0
                                                 tip:@"请输入"
                                         autoCorrect:NO
                                               focus:NO
                                                text:nil];
    
    [cellInfo addUserInfoValue:@( NSTextAlignmentRight )
                        forKey:CInfoEditorAlignKey];
    
    [cellInfo addUserInfoValue:@( UIKeyboardTypeDecimalPad )
                        forKey:CInfoEditorKeyboardTypeKey];
    
    UITextField *textF = [cellInfo getUserInfoValueForKey:CInfoEditorKey];
    textF.delegate = self;
    
    [secInfo addCell:cellInfo];
    [_m_tableViewInfo addSection:secInfo];
    
    [[_m_tableViewInfo getTableView] reloadData];
    
}

- (void)dealloc {
    [self removeKeyboardNotification];
}

#pragma mark - Actions
- (void)onBottomBtnAction:(__unused id)sender {
    
    IBTTableViewSectionInfo *secInfo = [_m_tableViewInfo getSectionAt:0];
    NSUInteger uiCellCount = [secInfo getCellCount];
    if (uiCellCount == 0) {
        return;
    }
    
    IBTTableViewCellInfo *cellInfo;
    
    BOOL bIsAllInputed = YES;
    NSMutableArray *mArrRecords = [NSMutableArray array];
    for (NSUInteger i = 0; i < uiCellCount; i ++) {
        cellInfo = [secInfo getCellAt:i];
        NSString *str = [cellInfo getUserInfoValueForKey:CInfoEditorTextKey];
        if (str.length == 0) {
            bIsAllInputed = NO;
            break;
        }
        [mArrRecords addObject:str];
    }
    
    if (bIsAllInputed) {
        if ([_m_delegate respondsToSelector:@selector(inputPopup:didAddRecords:)]) {
            [_m_delegate inputPopup:self didAddRecords:mArrRecords];
        }
        
        [self dismissPopup];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if ([string isEqualToString:@""]) {
        return YES;
    }
    else if ([string isEqualToString:@"."]) {
        // 判断 textField.text 是否为 纯数字
        if ([textField.text rangeOfString:@"."].location != NSNotFound) {
            return NO;
        }
        else {
            return YES;
        }
    }
    else if (textField.text.length == 1 &&
             [textField.text isEqualToString:@"0"])
    {
        BOOL bIsNum = [[self class] checkIsNumberWithStr:string];
        if (bIsNum) {
            if (![string isEqualToString:@"0"]) {
                textField.text = string;
            }
        }
        
        return NO;
    }
    
    return [[self class] checkIsNumberWithStr:string];
}

+ (BOOL)checkIsNumberWithStr:(NSString *)string {
    NSString *regexStr = @"^[0-9]\\d*$";
    
    NSError *error = nil;
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];
    if (string != nil && regex != nil) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
        if (firstMatch) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - Keyboard Notification

- (void)addKeyboardNotification {
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self
                   selector:@selector(keyboardWillShow:)
                       name:UIKeyboardWillShowNotification
                     object:nil];
    [notiCenter addObserver:self
                   selector:@selector(keyboardWillHide:)
                       name:UIKeyboardWillHideNotification
                     object:nil];
}

- (void)removeKeyboardNotification {
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter removeObserver:self
                          name:UIKeyboardWillShowNotification
                        object:nil];
    [notiCenter removeObserver:self
                          name:UIKeyboardWillHideNotification
                        object:nil];
}


#pragma mark - Keyboard
- (void)keyboardWillShow:(NSNotification *)note {
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self convertRect:keyboardBounds toView:nil];
    
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    // set views with new info
    
    self.y = self.superview.height - CGRectGetHeight(keyboardBounds) - self.height;
    
    // commit animations
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)note {
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    // set views with new info
    self.y = (self.superview.height - self.height) * .5f;
    
    // commit animations
    [UIView commitAnimations];
}


@end