1.首先我在AppDelegate.m里面
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//创建并初始化UITabBarController
RootViewController*tabBarController = [[RootViewControlleralloc]init];
self.window.rootViewController= tabBarController;
returnYES;
}
2.然后在我的TabBarController.m里面
//初始化四个视图控制器(控制器用了“one two three”,不要模仿)
HomeViewController*oneVc = [[HomeViewControlleralloc]init];
OneViewController*twoVc = [[OneViewControlleralloc]init];
TwoViewController*threeVc = [[TwoViewControlleralloc]init];
ThreeViewController*fourVc = [[ThreeViewControlleralloc]init];
//为四个视图控制器添加导航栏控制器
UINavigationController*navOne = [[UINavigationControlleralloc]initWithRootViewController:oneVc];
UINavigationController*navTwo = [[UINavigationControlleralloc]initWithRootViewController:twoVc];
UINavigationController*navThree = [[UINavigationControlleralloc]initWithRootViewController:threeVc];
UINavigationController*navFour = [[UINavigationControlleralloc]initWithRootViewController:fourVc];
//设置控制器文字
navOne.title=@"1";
navTwo.title=@"2";
navThree.title=@"3";
navFour.title=@"4";
//设置控制器图片(使用imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal,不被系统渲染成蓝色)
navOne.tabBarItem.image= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navOne.tabBarItem.selectedImage= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTwo.tabBarItem.image= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTwo.tabBarItem.selectedImage= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navThree.tabBarItem.image= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navThree.tabBarItem.selectedImage= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navFour.tabBarItem.image= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navFour.tabBarItem.selectedImage= [[UIImageimageNamed:@"icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//改变tabbarController文字选中颜色(默认渲染为蓝色)
[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorlightGrayColor]}forState:UIControlStateNormal];
[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorredColor]}forState:UIControlStateSelected];
//创建一个数组包含四个导航栏控制器
NSArray*vcArry = [NSArrayarrayWithObjects:navOne,navTwo,navThree,navFour,nil];
//将数组传给UITabBarController
self.viewControllers= vcArry;
- (BOOL)tabBarController:(UITabBarController*)tabBarController shouldSelectViewController:(UIViewController*)viewController{
//判断用户是否登陆
BOOLresult = [[NSUserDefaultsstandardUserDefaults]boolForKey:@"ziDong"];
NSIntegerselectedInde;
if(!result) {
if([viewController.tabBarItem.titleisEqualToString:@"1"]) {
selectedInde =0;
}elseif([viewController.tabBarItem.titleisEqualToString:@"2"]) {
selectedInde =1;
}elseif([viewController.tabBarItem.titleisEqualToString:@"3"]) {
selectedInde =2;
}else{
selectedInde =3;
}
//这里拿到你想要的tabBarItem,这里的方法有很多,还有通过tag值
if([viewController.tabBarItem.titleisEqualToString:@"3"]) {
LoginViewController*detail = [[LoginViewControlleralloc]init];
detail.selectedInde= selectedInde;
[((UINavigationController*)tabBarController.selectedViewController)presentViewController:detailanimated:YEScompletion:nil];
//在登陆界面判断登陆成功之后发送通知,将所选的TabbarItem传回,使用通知传值
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(logSelect:)name:@"logSelect"object:nil];//接收
//这里的NO是关键,如果是这个tabBarItem,就不要让他点击进去
returnNO;
}
}
//当然其余的还是要点击进去的
returnYES;
}
- (void)logSelect:(NSNotification*)text{
self.selectedIndex= [text.userInfo[@"logSelect"]integerValue];;
}
3.创建登录页面这里我只写了一个登录按钮
#import"LoginViewController.h"
#import"TwoViewController.h"(要点击的那个tabbar所在的页面,这个页面我在这就不写了)
@interfaceLoginViewController()
@end
@implementationLoginViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//颜色
self.view.backgroundColor= [UIColorcolorWithString:@"#eeeeee"];
self.navigationItem.title=@"登录";
//返回
UIButton*backBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
backBtn.frame=CGRectMake(0,20,60,40);
[backBtnsetTitle:@"返回"forState:UIControlStateNormal];
[backBtnsetImage:[UIImageimageNamed:@"返回"]forState:UIControlStateNormal];
[backBtnsetTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];
[backBtnaddTarget:selfaction:@selector(back:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:backBtn];
UIButton*btnLogin = [UIButtonbuttonWithType:UIButtonTypeCustom];
btnLogin.frame=CGRectMake(100,300,100,30);
btnLogin.backgroundColor= [UIColorbrownColor];
[self.viewaddSubview:btnLogin];
[btnLogin setTitle:@"登录"forState:UIControlStateNormal];
[btnLogin addTarget:selfaction:@selector(btnLoginAction)forControlEvents:UIControlEventTouchUpInside];
}
//返回
- (void)back:(UIButton*)btn {
[self dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)btnLoginAction {
BOOLresult = [[NSUserDefaultsstandardUserDefaults]boolForKey:@"ziDong"];
if(!result){
[[NSUserDefaultsstandardUserDefaults]setBool:YESforKey:@"ziDong"];
}else
{
[[NSUserDefaultsstandardUserDefaults]setBool:NOforKey:@"ziDong"];
}
NSString*str = [NSStringstringWithFormat:@"%ld",self.selectedInde];
NSDictionary*dict =@{@"logSelect":str};
[[NSNotificationCenterdefaultCenter]postNotificationName:@"logSelect"object:niluserInfo:dict];
[selfdismissViewControllerAnimated:YEScompletion:^{
NSLog(@"-----%ld",self.selectedInde);
}];
}
4.退出按钮的地方点击方法里
[[NSUserDefaultsstandardUserDefaults]setBool:NOf orKey:@"ziDong"];