CustomTabbarController.m 12.2 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11

//
//  CustomTabbarController.m
//  Lighting
//
//  Created by 曹云霄 on 16/4/27.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CustomTabbarController.h"
#import "Toolview.h"
曹云霄's avatar
曹云霄 committed
12
#import "AppDelegate.h"
13 14 15
#import "SceneLibraryViewController.h"
#import "ProductLibraryViewController.h"
#import "AboutViewController.h"
曹云霄's avatar
曹云霄 committed
16 17
#import "AllCustomerViewController.h"
#import "CustomerOrderViewController.h"
曹云霄's avatar
曹云霄 committed
18
#import "UserViewController.h"
19
#import "ChangePasswordViewController.h"
勾芒's avatar
勾芒 committed
20
#import "SearchViewController.h"
勾芒's avatar
勾芒 committed
21
#import "BaseViewController.h"
勾芒's avatar
勾芒 committed
22
#import "QRViewController.h"
曹云霄's avatar
曹云霄 committed
23
#import "ExperienceCentreViewController.h"
曹云霄's avatar
曹云霄 committed
24
#import "RebateViewController.h"
曹云霄's avatar
曹云霄 committed
25 26 27 28
#import "GuideIntegralViewController.h"
#import "LearningCenterMainViewController.h"
#import "AnnouncementViewController.h"

勾芒's avatar
勾芒 committed
29 30

@interface CustomTabbarController ()<TabbarButtonClickdelegate,ChangpasswordDelegate,CancelButtondelegate,UITextFieldDelegate>
曹云霄's avatar
曹云霄 committed
31 32

@property (nonatomic,strong) NSArray *identifierArray;
曹云霄's avatar
曹云霄 committed
33

34 35 36 37 38
/**
 *  保存按钮引用
 */
@property (nonatomic,strong) UIButton *Newbutton;

勾芒's avatar
勾芒 committed
39 40 41
/**
 *  控制器数组
 */
42
@property (nonatomic,strong) NSMutableArray *vcArray;
勾芒's avatar
勾芒 committed
43

曹云霄's avatar
曹云霄 committed
44 45 46 47 48
/**
 *  工具栏
 */
@property (nonatomic,strong) Toolview *toolview;

曹云霄's avatar
曹云霄 committed
49 50 51 52
@end

@implementation CustomTabbarController

曹云霄's avatar
曹云霄 committed
53 54 55 56 57 58 59 60 61
/**
 *  UIStoryboard Identifier数组
 *
 *  @return NSArary
 */
- (NSArray *)identifierArray
{
    if (_identifierArray == nil) {
        
曹云霄's avatar
曹云霄 committed
62
        _identifierArray = [NSArray arrayWithObjects:@"SearchViewController",@"ShoppingViewController",@"ClientViewController",@"RebateViewController",@"GuideIntegralViewController",@"AnnouncementViewController",@"SceneLibraryViewController",@"ProductLibraryViewController",@"AllCustomerViewController",@"CustomerOrderViewController",@"LearningCenterMainViewController",@"AboutViewController", nil];
曹云霄's avatar
曹云霄 committed
63
    }
勾芒's avatar
勾芒 committed
64
    return _identifierArray;
曹云霄's avatar
曹云霄 committed
65 66
}

67 68 69 70 71 72 73 74
- (NSMutableArray *)vcArray
{
    if (!_vcArray) {
        _vcArray = [NSMutableArray array];
    }
    return _vcArray;
}

曹云霄's avatar
曹云霄 committed
75

勾芒's avatar
勾芒 committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.selectedViewController endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.selectedViewController endAppearanceTransition];
}

曹云霄's avatar
曹云霄 committed
100 101
- (void)viewDidLoad {
    [super viewDidLoad];
102
    
曹云霄's avatar
曹云霄 committed
103 104 105 106 107 108 109
    [self uiConfigAction];
    [self addViewcontroller];
}

#pragma mark -UI
- (void)uiConfigAction
{
110 111
    self.tabBar.frame = CGRectMake(ZERO, ZERO, ScreenWidth, NavigationHeight);
    self.toolview = [[Toolview alloc]initWithFrame:CGRectMake(ZERO, ZERO, ScreenWidth, NavigationHeight)];
曹云霄's avatar
曹云霄 committed
112 113
    self.toolview.delegate = self;
    self.toolview.inputField.delegate = self;
勾芒's avatar
勾芒 committed
114
    self.delegate = self;
115 116
    [self.view addSubview:self.toolview];
    self.tabBar.hidden = YES;
勾芒's avatar
勾芒 committed
117
    //显示体验中心
曹云霄's avatar
曹云霄 committed
118
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ShowFollowHeart:) name:OPENFOLLOWHEARTVC object:nil];
曹云霄's avatar
曹云霄 committed
119 120 121
}


122
#pragma mark -添加controller到viewControllers
曹云霄's avatar
曹云霄 committed
123 124
- (void)addViewcontroller
{
125
    NSMutableArray *controllerArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
126
    UIStoryboard *storyboard = nil;
127
    for (int i=0; i<self.identifierArray.count; i++) {
曹云霄's avatar
曹云霄 committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141
        
        NSString *controllString = self.identifierArray[i];
        // 公告
        if ([controllString isEqualToString:@"AnnouncementViewController"]) {
            storyboard = [BaseViewController getAnnouncementStoryboardClass];
        }else if ([controllString isEqualToString:@"LearningCenterMainViewController"]) {
            // 学习中心
            storyboard = [BaseViewController getLearningCenterStoryboardClass];
        }else if ([controllString isEqualToString:@"GuideIntegralViewController"]) {
            // 积分
            storyboard = [BaseViewController getGuideIntegralStoryboardClass];
        }else {
            storyboard = [BaseViewController getMainStoryboardClass];
        }
142 143 144 145 146
        BaseViewController *control = [storyboard instantiateViewControllerWithIdentifier:self.identifierArray[i]];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:control];
        [self.vcArray addObject:nav];
        [controllerArray addObject:control];
    }
勾芒's avatar
勾芒 committed
147
    self.viewControllers = self.vcArray;
148
    SHARED_APPDELEGATE.allControllerArray = controllerArray;
曹云霄's avatar
曹云霄 committed
149
    SHARED_APPDELEGATE.tabBarController = self;
150
    self.selectedIndex = 2;
151 152 153 154 155 156 157 158 159 160 161
}


#pragma mark -移除系统自带的UITabBarButton
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
曹云霄's avatar
曹云霄 committed
162
}
曹云霄's avatar
曹云霄 committed
163

曹云霄's avatar
曹云霄 committed
164 165

#pragma amrk -TabbarButtonClickdelegate代理
曹云霄's avatar
曹云霄 committed
166
- (void)ButtonClickAction:(NSInteger)Buttontag withButton:(UIButton *)button
曹云霄's avatar
曹云霄 committed
167
{
曹云霄's avatar
曹云霄 committed
168 169
    [self.toolview.inputField resignFirstResponder];
    [self dismissViewControllerAnimated:NO completion:nil];
170
    _Newbutton = button;
曹云霄's avatar
曹云霄 committed
171 172
    switch (Buttontag) {
            
曹云霄's avatar
曹云霄 committed
173
            //右侧视图
曹云霄's avatar
曹云霄 committed
174 175 176 177 178
        case 100:
            
            [SHARED_APPDELEGATE.mmdrawer toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
            
            break;
曹云霄's avatar
曹云霄 committed
179
            
曹云霄's avatar
曹云霄 committed
180
            //公告
曹云霄's avatar
曹云霄 committed
181
        case 101:
曹云霄's avatar
曹云霄 committed
182
            
曹云霄's avatar
曹云霄 committed
183 184 185 186 187 188 189 190
        {
            self.selectedIndex = 5;
        }
            break;
            
            //某某用户
        case 102:
            
曹云霄's avatar
曹云霄 committed
191 192
        {
            UserViewController *userVC = [[UserViewController alloc]init];
193
            userVC.delegate = self;
194
            userVC.preferredContentSize = CGSizeMake(260, 230);
曹云霄's avatar
曹云霄 committed
195 196 197 198
            userVC.modalPresentationStyle = UIModalPresentationPopover;
            UIPopoverPresentationController *pop = userVC.popoverPresentationController;
            pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
            pop.sourceView = userVC.view;
199
            pop.barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
曹云霄's avatar
曹云霄 committed
200 201
            [self presentViewController:userVC animated:YES completion:nil];
        }
曹云霄's avatar
曹云霄 committed
202
            break;
曹云霄's avatar
曹云霄 committed
203 204
            
            //我的客户
曹云霄's avatar
曹云霄 committed
205
        case 103:
曹云霄's avatar
曹云霄 committed
206
            
207
            self.selectedIndex = 2;
曹云霄's avatar
曹云霄 committed
208 209
            
            break;
曹云霄's avatar
曹云霄 committed
210 211
            
            //购物车
曹云霄's avatar
曹云霄 committed
212
        case 104:
曹云霄's avatar
曹云霄 committed
213
            
勾芒's avatar
勾芒 committed
214 215 216
            //必须设置当前客户才能跳转到购物车
            if (![Shoppersmanager manager].currentCustomer) {
                
曹云霄's avatar
曹云霄 committed
217
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];
218
                hud.labelText = @"必须设置当前客户才能访问购物车";
勾芒's avatar
勾芒 committed
219
                hud.margin = 10.f;
220
                hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.5];
勾芒's avatar
勾芒 committed
221 222 223 224
                hud.minSize = CGSizeMake(200, 50);
                hud.animationType = MBProgressHUDAnimationZoom;
                hud.mode = MBProgressHUDModeText;
                hud.removeFromSuperViewOnHide = YES;
225
                [hud hide:YES afterDelay:1.0f];
勾芒's avatar
勾芒 committed
226 227
            }else
            {
勾芒's avatar
勾芒 committed
228
                
229
                self.selectedIndex = 1;
勾芒's avatar
勾芒 committed
230
            }
曹云霄's avatar
曹云霄 committed
231 232 233 234 235
            
            break;
            
        default:
            break;
曹云霄's avatar
曹云霄 committed
236
    }
曹云霄's avatar
曹云霄 committed
237 238
}

239 240 241 242 243 244 245 246 247 248 249 250 251
#pragma mark -修改密码点击
- (void)changPasswordButtonClick
{
    [self dismissViewControllerAnimated:NO completion:^{
        
        ChangePasswordViewController *changpassword = [[ChangePasswordViewController alloc]init];
        changpassword.delegate = self;
        changpassword.preferredContentSize = CGSizeMake(260, 180);
        changpassword.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController *pop = changpassword.popoverPresentationController;
        pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
        pop.sourceView = changpassword.view;
        pop.barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_Newbutton];
252
        [self presentViewController:changpassword animated:YES completion:nil];
253
    }];
曹云霄's avatar
曹云霄 committed
254
}
255 256 257 258 259 260 261 262 263


#pragma mark -取消按钮
- (void)cancelButtonClick
{
    [self dismissViewControllerAnimated:NO completion:^{
        
        UserViewController *userVC = [[UserViewController alloc]init];
        userVC.delegate = self;
264
        userVC.preferredContentSize = CGSizeMake(260, 230);
265 266 267 268 269 270 271 272 273
        userVC.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController *pop = userVC.popoverPresentationController;
        pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
        pop.sourceView = userVC.view;
        pop.barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_Newbutton];
        [self presentViewController:userVC animated:YES completion:nil];
    }];
}

曹云霄's avatar
曹云霄 committed
274 275 276
#pragma mark -二维码扫描
- (void)QrcodeButtonClick
{
曹云霄's avatar
曹云霄 committed
277
    WS(weakSelf);
278 279
    // 判断应用是否有使用相机的权限
    if(![BaseViewController determineCameraPermissions]){
曹云霄's avatar
曹云霄 committed
280
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在iPad的“设置-隐私-相机”选项中,允许欧立方访问你的相机" preferredStyle:UIAlertControllerStyleAlert];
281
        [alertVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
曹云霄's avatar
曹云霄 committed
282 283 284
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }else {
285
        
曹云霄's avatar
曹云霄 committed
286
        QRViewController *qrVC = [[QRViewController alloc] initWithScanCompleteHandler:^(NSString *url) {
曹云霄's avatar
曹云霄 committed
287
            [weakSelf dismissViewControllerAnimated:YES completion:^{
曹云霄's avatar
曹云霄 committed
288 289 290
                
                ProductLibraryViewController *product = [SHARED_APPDELEGATE.allControllerArray objectAtIndex_opple:4];
                product.barcode = url;
曹云霄's avatar
曹云霄 committed
291
                weakSelf.selectedIndex = 3;
曹云霄's avatar
曹云霄 committed
292
            }];
293
        }];
曹云霄's avatar
曹云霄 committed
294 295 296 297
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf presentViewController:qrVC animated:YES completion:nil];
        });
    }
勾芒's avatar
勾芒 committed
298 299
}

勾芒's avatar
勾芒 committed
300

勾芒's avatar
勾芒 committed
301 302 303
#pragma mark -搜索框代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
304
    self.selectedIndex = 0;
勾芒's avatar
勾芒 committed
305 306 307 308 309 310 311 312
    return YES;
}


#pragma mark -Search按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //搜索
313
    if (![BaseViewController isBlankString:textField.text]) {
勾芒's avatar
勾芒 committed
314
        [textField resignFirstResponder];
勾芒's avatar
勾芒 committed
315 316
        //搜索
        [[NSNotificationCenter defaultCenter] postNotificationName:SEARCHSTRING object:textField.text];
勾芒's avatar
勾芒 committed
317 318
    }
    return YES;
曹云霄's avatar
曹云霄 committed
319 320
}

勾芒's avatar
勾芒 committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
#pragma mark -自定义选中
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    [self tabBarController:self didSelectViewController:[self.vcArray objectAtIndex_opple:selectedIndex]];
}


#pragma mark -回到最上层
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *navVC = (UINavigationController *)viewController;
        [navVC popToRootViewControllerAnimated:NO];
    }
}

勾芒's avatar
勾芒 committed
339 340 341
#pragma mark -推出体验中心控制器
- (void)ShowFollowHeart:(NSNotification *)objc
{
曹云霄's avatar
曹云霄 committed
342 343 344
    if (![Shoppersmanager manager].currentCustomer) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];
曹云霄's avatar
曹云霄 committed
345
        hud.labelText = @"必须设置当前客户才能访问体验中心";
曹云霄's avatar
曹云霄 committed
346
        hud.margin = 10.f;
347
        hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.5];
曹云霄's avatar
曹云霄 committed
348 349 350 351
        hud.minSize = CGSizeMake(200, 50);
        hud.animationType = MBProgressHUDAnimationZoom;
        hud.mode = MBProgressHUDModeText;
        hud.removeFromSuperViewOnHide = YES;
352
        [hud hide:YES afterDelay:1.0f];
曹云霄's avatar
曹云霄 committed
353 354 355
        
    }else
    {
曹云霄's avatar
曹云霄 committed
356
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"OppleMain" bundle:nil];
357 358 359 360 361
        ExperienceCentreViewController *ExperienceCenter = [storyboard instantiateViewControllerWithIdentifier:@"ExperienceCentre"];
        ExperienceCenter.modalPresentationStyle = UIModalPresentationOverFullScreen;
        UIPopoverPresentationController *popover = ExperienceCenter.popoverPresentationController;
        popover.sourceView = ExperienceCenter.view;
        [self presentViewController:ExperienceCenter animated:YES completion:nil];
曹云霄's avatar
曹云霄 committed
362
    }
勾芒's avatar
勾芒 committed
363 364 365
}


曹云霄's avatar
曹云霄 committed
366
@end