zoukankan      html  css  js  c++  java
  • UI1_UITabBarController

    //
    //  AppDelegate.h
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import <CoreData/CoreData.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
    @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
    
    - (void)saveContext;
    - (NSURL *)applicationDocumentsDirectory;
    
    
    @end
    
    
    //
    //  AppDelegate.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "LimitViewController.h" //限免
    #import "FreeViewController.h"  //免费
    #import "ReduceViewController.h"//降价
    #import "SubjectViewController.h"//专题
    #import "HotHankViewController.h"//热榜
    #import "AccountViewController.h"//账户
    #import "SystemViewController.h" //系统
    
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        //标签栏控制器
        //通常在一个工程中只有一个标签栏控制器, 标签控制器作为window的根视图控制器
        
        LimitViewController *limitVC = [[LimitViewController alloc] init];
        UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:limitVC];
        
        //标签栏的标题
        
        //nav1.tabBarItem.title = @"XXX";
        //nav1.navigationItem.title = @"限免";
        //设置title 同时设置tabBarItem.title 和 navigationItem.title
        nav1.title = @"限免";
        NSLog(@"title = %@ title = %@", nav1.tabBarItem.title, nav1.title);
        //NSLog(@"title = %@ title = %@", nav1.title,nav1.navigationItem.title);
        //NSLog(@"%p %p", nav1.tabBarItem.title, nav1.navigationItem.title);
        
        
        //设置选中状态的图片
        nav1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_limitfree_press@2x"];
        //设置非选中状态的图片
        nav1.tabBarItem.image = [UIImage imageNamed:@"tabbar_limitfree@2x"];
        nav1.tabBarItem.badgeValue = @"10";
        
        
        
        FreeViewController *freeVC = [[FreeViewController alloc] init];
        UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:freeVC];
        nav2.title = @"免费";
        nav2.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_appfree_press@2x"];
        nav2.tabBarItem.image = [UIImage imageNamed:@"tabbar_appfree@2x"];
        //设置微标
        nav2.tabBarItem.badgeValue = @"5";
        
        
        ReduceViewController *reduceVC = [[ReduceViewController alloc] init];
        UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:reduceVC];
        nav3.title = @"降价";
        
        UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"降价" image:[UIImage imageNamed:@"tabbar_reduceprice@2x"] selectedImage:[UIImage imageNamed:@"tabbar_reduceprice_press@2x"]];
        nav3.tabBarItem = item3;
        
        
        SubjectViewController *subjectVC = [[SubjectViewController alloc] init];
        subjectVC.title = @"专题";
        
        UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"专题" image:[UIImage imageNamed:@"tabbar_subject@2x"] selectedImage:[UIImage imageNamed:@"tabbar_subject_press@2x"]];
        subjectVC.tabBarItem = item4;
        
        
        HotHankViewController *hotHankVC = [[HotHankViewController alloc] init];
        hotHankVC.title = @"热榜";
        UITabBarItem *item5 = [[UITabBarItem alloc] initWithTitle:@"热榜" image:[UIImage imageNamed:@"tabbar_rank@2x"] tag:0];
        hotHankVC.tabBarItem = item5;
        hotHankVC.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_rank_press@2x"];
        
        AccountViewController *accountVC = [[AccountViewController alloc] init];
        accountVC.title = @"账户";
        
        accountVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_account@2x"];
        accountVC.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_account_press@2x"];
        
        SystemViewController *systemVC = [[SystemViewController alloc] init];
        //创建系统样式的tabBarItem
        UITabBarItem *item6 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
        systemVC.tabBarItem = item6;
        systemVC.title = @"书签";
        //NSLog(@"system = %@",systemVC.title);
        
        UITabBarController *tabController = [[UITabBarController alloc] init];
        
        NSArray *controllers = [NSArray arrayWithObjects:nav1,nav2,nav3,subjectVC,hotHankVC,accountVC,systemVC, nil];
        tabController.viewControllers = controllers;
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSArray *titles = [defaults objectForKey:@"titles"];
        NSMutableArray *newViewControllers =[NSMutableArray array];
        for (NSString *title in titles) {
            for (UIViewController *obj in controllers) {
                if ([title isEqualToString:obj.title]) {
                    [newViewControllers addObject:obj];
                }
            }
        }
        if ([newViewControllers count]) {
            tabController.viewControllers = newViewControllers;
        }
        //设置标签栏
        
        //设置活跃状态颜色
        //tabController.tabBar.tintColor = [UIColor redColor];
        NSLog(@"tabBar = %@", tabController.tabBar);
        //设置tabBar的背景图片
        tabController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_bg@2x"];
        //选中状态的指示图
        //tabController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"002"];
        //设置默认选中状态
        tabController.selectedIndex = 1;
        
        defaults = [NSUserDefaults standardUserDefaults];
        NSInteger index = [defaults integerForKey:@"selected"];
        if (index) {
            tabController.selectedIndex = index;
        }
        else
        {
            tabController.selectedIndex = 0;
        }
        
        //tabController.selectedViewController = subjectVC;
        //设置代理
        tabController.delegate = self;
        
        
        //标签栏控制器作为window的根视图控制
        self.window.rootViewController = tabController;
        // tabController.tabBar.hidden=YES;
        
        return YES;
    }
    
    #pragma mark ---tabBarControllerDelegate---
    
    //点击tabBarItem调用此方法
    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    {
        NSLog(@"shouldSelect!!!");
    
        return YES;
    }
    //选中tabBarItem调用此方法
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
        NSLog(@"did select!!!");
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setInteger:tabBarController.selectedIndex  forKey:@"selected"];
        [defaults synchronize];
    }
    
    //开始编辑时调用此方法
    - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
    {
        NSLog(@"开始编辑");
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
    {
        NSLog(@"将要结束编辑");
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
    {
        NSLog(@"结束编辑");
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray *titles = [NSMutableArray array];
        for (UIViewController *obj in tabBarController.viewControllers) {
            [titles addObject:obj.title];
        }
        [defaults setObject:titles forKey:@"titles"];
        [defaults synchronize];
    }
    
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        // Saves changes in the application's managed object context before the application terminates.
        [self saveContext];
    }
    
    #pragma mark - Core Data stack
    
    @synthesize managedObjectContext = _managedObjectContext;
    @synthesize managedObjectModel = _managedObjectModel;
    @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
    
    - (NSURL *)applicationDocumentsDirectory {
        // The directory the application uses to store the Core Data store file. This code uses a directory named "-000phone.com.UI1_UITabBarController" in the application's documents directory.
        return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    }
    
    - (NSManagedObjectModel *)managedObjectModel {
        // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
        if (_managedObjectModel != nil) {
            return _managedObjectModel;
        }
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"UI1_UITabBarController" withExtension:@"momd"];
        _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
        return _managedObjectModel;
    }
    
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
        // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
        if (_persistentStoreCoordinator != nil) {
            return _persistentStoreCoordinator;
        }
        
        // Create the coordinator and store
        
        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"UI1_UITabBarController.sqlite"];
        NSError *error = nil;
        NSString *failureReason = @"There was an error creating or loading the application's saved data.";
        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
            // Report any error we got.
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
            dict[NSLocalizedFailureReasonErrorKey] = failureReason;
            dict[NSUnderlyingErrorKey] = error;
            error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
            // Replace this with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
        
        return _persistentStoreCoordinator;
    }
    
    
    - (NSManagedObjectContext *)managedObjectContext {
        // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
        if (_managedObjectContext != nil) {
            return _managedObjectContext;
        }
        
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (!coordinator) {
            return nil;
        }
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
        return _managedObjectContext;
    }
    
    #pragma mark - Core Data Saving support
    
    - (void)saveContext {
        NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
        if (managedObjectContext != nil) {
            NSError *error = nil;
            if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
        }
    }
    
    @end
    
    //
    //  ViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "FreeViewController.h"
    
    @interface FreeViewController ()
    
    @end
    
    @implementation FreeViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor cyanColor];
        self.title = @"免费";
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    //
    //  HiddenViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "HiddenViewController.h"
    
    @interface HiddenViewController ()
    
    @end
    
    @implementation HiddenViewController
    
    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
            //隐藏标签栏 必须在视图控制器加载之前设置
            self.hidesBottomBarWhenPushed = YES;
        }
        return self;
    }
    
    - (void)dealloc
    {
        NSLog(@"hidden dealloc");
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor purpleColor];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    //
    //  SystemViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "SystemViewController.h"
    
    @interface SystemViewController ()
    
    @end
    
    @implementation SystemViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor yellowColor];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    //
    //  AccountViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "AccountViewController.h"
    
    @interface AccountViewController ()
    
    @end
    
    @implementation AccountViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor yellowColor];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    //
    //  HotHankViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "HotHankViewController.h"
    
    @interface HotHankViewController ()
    
    @end
    
    @implementation HotHankViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor blueColor];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    //
    //  SubjectViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "SubjectViewController.h"
    
    @interface SubjectViewController ()
    
    @end
    
    
    
    
    @implementation SubjectViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = [UIColor redColor];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    //
    //  LimitViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "LimitViewController.h"
    #import "HiddenViewController.h"
    
    @interface LimitViewController ()
    
    @end
    
    @implementation LimitViewController
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        self.hidesBottomBarWhenPushed = NO;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor whiteColor];
        self.title = @"限免";
        
        self.hidesBottomBarWhenPushed = YES;
        
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
        [webView loadRequest:request];
        [self.view addSubview:webView];
    
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked)];
        self.navigationItem.rightBarButtonItem = item;
    }
    
    - (void)btnClicked
    {
        HiddenViewController *hvc = [[HiddenViewController alloc] init];
        hvc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self.navigationController pushViewController:hvc animated:YES];
    }
    
    - (void)dealloc
    {
        NSLog(@"dealloc");
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    //
    //  ReduceViewController.m
    //  UI1_UITabBarController
    //
    //  Created by zhangxueming on 15/7/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "ReduceViewController.h"
    
    @interface ReduceViewController ()
    
    @end
    
    @implementation ReduceViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor greenColor];
        self.title = @"降价";
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
  • 相关阅读:
    python
    在liunx环境下安装python
    解决用navicate远程连接数据库出现1045
    mysql的监控及优化
    mysql基础
    linux基础学习
    Effective c++ 第一章 让自己习惯C++
    读前感
    socket编程:客户端与服务器间的连接以及各函数的用法
    生成任意区间的随机数
  • 原文地址:https://www.cnblogs.com/0515offer/p/4638840.html
Copyright © 2011-2022 走看看