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
#import <AVFoundation/AVFoundation.h>
勾芒's avatar
勾芒 committed
26 27

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

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

31 32 33 34 35
/**
 *  保存按钮引用
 */
@property (nonatomic,strong) UIButton *Newbutton;

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

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

曹云霄's avatar
曹云霄 committed
46 47 48 49
@end

@implementation CustomTabbarController

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

64 65 66 67 68 69 70 71
- (NSMutableArray *)vcArray
{
    if (!_vcArray) {
        _vcArray = [NSMutableArray array];
    }
    return _vcArray;
}

曹云霄's avatar
曹云霄 committed
72

勾芒's avatar
勾芒 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
-(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
97

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

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


121
#pragma mark -添加controller到viewControllers
曹云霄's avatar
曹云霄 committed
122 123
- (void)addViewcontroller
{
124
    NSMutableArray *controllerArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
125
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardwithCYX" bundle:nil];
126 127 128 129 130 131
    for (int i=0; i<self.identifierArray.count; i++) {
        BaseViewController *control = [storyboard instantiateViewControllerWithIdentifier:self.identifierArray[i]];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:control];
        [self.vcArray addObject:nav];
        [controllerArray addObject:control];
    }
勾芒's avatar
勾芒 committed
132
    self.viewControllers = self.vcArray;
133 134
    SHARED_APPDELEGATE.allControllerArray = controllerArray;
    self.selectedIndex = 2;
135 136 137 138 139 140 141 142 143 144 145
}


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

曹云霄's avatar
曹云霄 committed
148 149

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

215 216 217 218 219 220 221 222 223 224 225 226 227
#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];
228
        [self presentViewController:changpassword animated:YES completion:nil];
229
    }];
曹云霄's avatar
曹云霄 committed
230
}
231 232 233 234 235 236 237 238 239 240



#pragma mark -取消按钮
- (void)cancelButtonClick
{
    [self dismissViewControllerAnimated:NO completion:^{
        
        UserViewController *userVC = [[UserViewController alloc]init];
        userVC.delegate = self;
241
        userVC.preferredContentSize = CGSizeMake(260, 230);
242 243 244 245 246 247 248 249 250
        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
251 252 253
#pragma mark -二维码扫描
- (void)QrcodeButtonClick
{
曹云霄's avatar
曹云霄 committed
254
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    //判断权限
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(status !=AVAuthorizationStatusAuthorized) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在iPad的“设置-隐私-相机”选项中,允许欧立方访问你的相机" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSURL * url = [NSURL URLWithString:@"prefs:root=com.gomore.opple"];
            dispatch_after(0.2, dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication]openURL:url];
            });
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }else {
        QRViewController *qrVC = [[QRViewController alloc] initWithScanCompleteHandler:^(NSString *url) {
曹云霄's avatar
曹云霄 committed
272
            [weakSelf dismissViewControllerAnimated:YES completion:^{
曹云霄's avatar
曹云霄 committed
273 274 275
                
                ProductLibraryViewController *product = [SHARED_APPDELEGATE.allControllerArray objectAtIndex_opple:4];
                product.barcode = url;
曹云霄's avatar
曹云霄 committed
276
                weakSelf.selectedIndex = 3;
曹云霄's avatar
曹云霄 committed
277
            }];
278
        }];
曹云霄's avatar
曹云霄 committed
279 280 281 282
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf presentViewController:qrVC animated:YES completion:nil];
        });
    }
勾芒's avatar
勾芒 committed
283 284
}

勾芒's avatar
勾芒 committed
285

勾芒's avatar
勾芒 committed
286 287 288
#pragma mark -搜索框代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
289
    self.selectedIndex = 0;
勾芒's avatar
勾芒 committed
290 291 292 293 294 295 296 297
    return YES;
}


#pragma mark -Search按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //搜索
298
    if (![BaseViewController isBlankString:textField.text]) {
勾芒's avatar
勾芒 committed
299
        [textField resignFirstResponder];
勾芒's avatar
勾芒 committed
300 301
        //搜索
        [[NSNotificationCenter defaultCenter] postNotificationName:SEARCHSTRING object:textField.text];
勾芒's avatar
勾芒 committed
302 303
    }
    return YES;
曹云霄's avatar
曹云霄 committed
304 305
}

勾芒's avatar
勾芒 committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
#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
324 325 326
#pragma mark -推出体验中心控制器
- (void)ShowFollowHeart:(NSNotification *)objc
{
曹云霄's avatar
曹云霄 committed
327 328 329
    if (![Shoppersmanager manager].currentCustomer) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];
曹云霄's avatar
曹云霄 committed
330
        hud.labelText = @"必须设置当前客户才能访问体验中心";
曹云霄's avatar
曹云霄 committed
331
        hud.margin = 10.f;
332
        hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.5];
曹云霄's avatar
曹云霄 committed
333 334 335 336
        hud.minSize = CGSizeMake(200, 50);
        hud.animationType = MBProgressHUDAnimationZoom;
        hud.mode = MBProgressHUDModeText;
        hud.removeFromSuperViewOnHide = YES;
337
        [hud hide:YES afterDelay:1.0f];
曹云霄's avatar
曹云霄 committed
338 339 340
        
    }else
    {
曹云霄's avatar
曹云霄 committed
341
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardwithCYX" bundle:nil];
342 343 344 345 346
        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
347
    }
勾芒's avatar
勾芒 committed
348 349
}

曹云霄's avatar
曹云霄 committed
350 351
#pragma mark - 打开相应的控制器
- (void)openControllerWithIndex:(NSNotification *)sender
曹云霄's avatar
曹云霄 committed
352
{
曹云霄's avatar
曹云霄 committed
353
    NSInteger index = [sender.object integerValue];
曹云霄's avatar
曹云霄 committed
354
    [self dismissViewControllerAnimated:NO completion:nil];
曹云霄's avatar
曹云霄 committed
355
    self.selectedIndex = index;
曹云霄's avatar
曹云霄 committed
356
}
勾芒's avatar
勾芒 committed
357

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