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
| #import "AppDelegate.h" #import "RootViewController.h" #import "ViewController1.h" #import "ViewController2.h" #import "ViewController3.h" #import "ViewController4.h" #import "ViewController5.h" #import "ViewController6.h" @interface AppDelegate ()<UITabBarControllerDelegate> @end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window setBackgroundColor:[UIColor whiteColor]]; [self createTabBarController]; [self.window makeKeyAndVisible]; return YES; }
-(void)createTabBarController{
UITabBarController *tabBarVc = [[UITabBarController alloc]init];
tabBarVc.delegate = self; self.window.rootViewController = tabBarVc;
ViewController1 *vc1 = [[ViewController1 alloc]init]; ViewController2 *vc2 = [[ViewController2 alloc]init]; ViewController3 *vc3 = [[ViewController3 alloc]init]; ViewController4 *vc4 = [[ViewController4 alloc]init]; ViewController5 *vc5 = [[ViewController5 alloc]init]; ViewController6 *vc6 = [[ViewController6 alloc]init];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:11]; vc1.tabBarItem = item1; UITabBarItem *item2 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:12]; vc2.tabBarItem = item2; UITabBarItem *item3 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:13]; vc3.tabBarItem = item3; UITabBarItem *item4 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:14]; vc4.tabBarItem = item4; UITabBarItem *item5 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:15]; vc5.tabBarItem = item5; UITabBarItem *item6 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:16]; vc6.tabBarItem = item6;
tabBarVc.tabBar.tintColor = [UIColor redColor];
tabBarVc.tabBar.translucent = NO;
tabBarVc.viewControllers = @[vc1,vc2,vc3,vc4,vc5,vc6];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUInteger index = [defaults integerForKey:@"index"]; NSLog(@"%ld",index);
tabBarVc.selectedIndex = index;
[self order]; } #pragma mark - #pragma mark UITabBarController delegate -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ NSLog(@"将要选中视图控制器"); if (viewController.tabBarItem.tag == 13) { return NO; }
return YES; } -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ NSLog(@"已经选中指定的视图控制器"); NSArray *array = tabBarController.viewControllers; NSLog(@"array = %@",array); NSUInteger index = [array indexOfObject:viewController];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:index forKey:@"index"];
[defaults synchronize]; } -(void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers{ NSLog(@"willBeginCustomizingViewControllers"); } -(void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{ NSLog(@"willEndCustomizingViewControllers"); } -(void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{ NSLog(@"didEndCustomizingViewControllers"); NSMutableArray *array = [NSMutableArray array];
if (changed == YES) { for (UIViewController *vc in viewControllers) { [array addObject:vc.title]; } } NSLog(@"%@",array); NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:array forKey:@"order"]; [defaults synchronize]; }
-(void)order{ UITabBarController * tbc = (UITabBarController*)self.window.rootViewController; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *titleArray = [defaults objectForKey:@"order"]; if (titleArray == nil) { return; } NSMutableArray *orderArray = [NSMutableArray array]; for (NSString *titler in titleArray) { for (UIViewController *vc in tbc.viewControllers) { if ([titler isEqualToString:vc.title]) { [orderArray addObject:vc]; break; } } } tbc.viewControllers = orderArray; }
@end
#import "BaseViewController.h"
@interface BaseViewController ()
@end
@implementation BaseViewController -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSStringFromClass([self class]); } return self; } - (void)viewDidLoad { [super viewDidLoad];
#define Random arc4random()%256/255.0 self.view.backgroundColor = [UIColor colorWithRed:Random green:Random blue:Random alpha:0.9f]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)]; NSString *name = NSStringFromClass([self class]); label.text = name; [self.view addSubview:label]; } @end
|