iOS 点击tabBar弹出登录窗口

之前自己也写过一次,但是无意中发现系统自带的更简单明了,因而优化下自己的代码,以后也自己长个记性–>多看苹果API,好吧,言归正传!!!
1、新建tabbar类,继承于UITabBarController
//这个属性是用来记录跳转窗口出来之前选择的index
@property (nonatomic, assign)NSInteger oldSelectIndex;
2、viewdidload里面实现UITabBarController的代理
self.delegate = self;
//UITabBarControllerDelegate
-(BOOL)tabBarController:(UITabBarController*)tabBarController shouldSelectViewController:(UIViewController *)viewController
{

self.oldSelectIndex = tabBarController.selectedIndex;
// 记录之前选择的selectedIndex
return YES;

}

/* 在 tabBar 切换前 shouldSelectViewController 获取到旧的界面的 index ,在切换的 delegate , didSelectViewController 中加入判断,若已经登录,不做操作,若未登录,将当前的 tabBarController 的 selectedIndex 设置为 self.oldSelectIndex ,登录成功之后再把 tabBarController 的 selectedIndex 设置为所需显示的界面,失败不做操作
*/
-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
{

if (tabBarController.selectedIndex == 1) {
//此处判断是否登录、如果未登录需要执行弹出登录页面的操作
    if (![LogInDataManager isLogin]) {
        //未登录将tabbar的selectedIndex设置为之前选择的selectedIndex
        tabBarController.selectedIndex = self.oldSelectIndex;
        [self presentLoginViewController];

    } else {//不做任何判断

    }
}

}

还有个问题当登录或者注册成功之后销毁登录或注册页面视图时候,我是直接发送通知进行的,但是这样写,当注册成功的时候登录页面会闪出来下

-(IBAction)logInAction:(id)sender
{

  [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissVC" object:nil];
  //登录成功发送销毁视图通知

}
-(IBAction)registAction:(id)sender
{

  [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissVC" object:nil];
  //注册成功销毁视图

}
TabBar类里面销毁视图
-(void)viewDidLoad
{

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismissLogin:) name:@"dismissVC" object:nil];

}
-(void)dismissLogin:(NSNotification *)noti
{

[self dismissViewControllerAnimated:YES completion:nil];

}
这样做的话当注册完成后登录页面会闪下。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值