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
//
// KWMGiftCardVC.m
// iCemarose
//
// Created by HouWeiBin on 16/9/27.
// Copyright © 2016年 kollway. All rights reserved.
//
#import "KWMAddGiftCardVC.h"
#import "KWMStringUtil.h"
@interface KWMAddGiftCardVC ()
@end
@implementation KWMAddGiftCardVC
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"输入礼品卡";
[self initDiscountTF];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)];
[self.view addGestureRecognizer:tapGesture];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
+(NSString *)kwmTag{
return @"KWMAddGiftCardVC";
}
//初始化優惠碼textfiled
- (void)initDiscountTF{
UILabel *leftPaddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 25)];
UILabel *rightPaddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
self.tfDiscount.leftView = leftPaddingView;
self.tfDiscount.rightView = rightPaddingView;
self.tfDiscount.leftViewMode = UITextFieldViewModeAlways;
self.tfDiscount.rightViewMode = UITextFieldViewModeAlways;
[self.tfDiscount addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
self.tfDiscount.delegate = self;
}
- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
[self.tfDiscount resignFirstResponder];
}
-(void)textFieldDidChange :(UITextField *)theTextField{
if([KWMStringUtil isEmpty:theTextField.text]){
self.btnClear.hidden = YES;
}else{
self.btnClear.hidden = NO;
}
NSLog( @"text changed: %@", theTextField.text);
}
-(void)onClickClear:(id)sender{
self.tfDiscount.text = @"";
self.btnClear.hidden = YES;
}
-(void)onClickAdd:(id)sender{
NSString *code = self.tfDiscount.text;
if([KWMStringUtil isEmpty:code] || code.length<8){
[self showToast:@"请至少输入8位礼品码~"];
}else{
[self addGiftCard:code];
}
}
-(void)addGiftCard:(NSString *)code{
if([KWMStringUtil isEmpty:code]){
return;
}
[self showLoading];
[self.client applyGiftCardCode:code toCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
[self hideLoading];
if (error == nil && checkout) {
[self showToast:@"添加优惠码成功"];
NSLog(@"Successfully added gift card");
self.checkout = checkout;
if(self.delegate != nil){
[self.delegate kwm_onClickAddGift:checkout];
}
[self.navigationController popViewControllerAnimated:YES];
}
else {
[self showToast:@"添加优惠码失败"];
[self showError:error];
NSLog(@"Error applying gift card: %@", error);
}
}];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
@end