prevent使用带标签栏控制器UIViewControllers(分镜) [英] Prevent access to UIViewControllers with tab bar controller (storyboard)

查看:170
本文介绍了prevent使用带标签栏控制器UIViewControllers(分镜)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到与我的iOS应用程序的问题。结果
我试图融入一个渐进的登录模式,即:使用可以访问某些应用程序没有被登录必需的。

I currently encountering a problem with my iOS application.
I am attempting to incorporate a gradual login pattern, i.e.: the use can access some of the app without being required to login.

必需的特点如下:


  • 在任何时候,用户可以查看需要登录的所有导航项目

  • 当用户试图访问要求登录一个的UIView(控制器),他们将与UIAlertView中提示,要求他们登录。(当应用程序识别的启动SEGUE目的地限制preferably的UIAlertView中会出现)。

起初,我用的UIViewController的一个子类,在指定的初始化器(initWith codeR),将NSUserDefaults的检查,看看如果用户登录。然后我关子类的那。限制如下:

At first I used a subclass of UIViewController that, in the designated initialiser (initWithCoder), would check NSUserDefaults to see if the user was logged in. I then subclassed off of that. Limitations were as follows:


  • 无法使用的UIViewController的其他子类,分别的UITableViewController

  • 的UIAlertView中想出的观点已经出现之后,这我假设将导致错误的子类的UIViewController假定用户已登录。

问题总结:结果
我想知道如何有条件p $ pvent $用户访问特定的UIView(控制器)S和的UIViewController的子类,而当这种情况发生present一个UIAlertView中。

Question summary:
I would like to know how to conditionally prevent users from accessing certain UIView(Controller)s and subclasses of UIViewController, and when that happens present a UIAlertView.

更新1 结果
可能的类别和/或协议是一个可行的解决方案?

Update 1
Could categories and/or protocols be a viable solution?

更新2 结果
CodaFi指出,单身作为一个伟大的解决方案来管理用户的状态。

Update 2
CodaFi pointed out singletons as a great solution to manage the user's state.

通过了实施我现在需要弄清楚如何控制用户的访问。结果
由于我使用的故事板,我觉得最通用的实现将继承UIStoryboardSegue,以及如果用户试图访问受限制的UIViewController(也许是受限制的控制器都有指定要求的状态的协议属性执行方法检查:登录/出)。然而这里的陷阱是你不能选择在故事板图形编辑器类/一UIStoryboardSegue的子类。我知道我能做到这一点编程,但是这似乎有些单调乏味,因为我必须添加IBActions等任务,以执行塞格斯方法,而且我不认为会与方式等元素navigationController和tabbarControllers行为工作。

With that implemented I now need to figure out how to control the user's access.
As I am using storyboards I feel that the most versatile implementation would be subclassing UIStoryboardSegue, and on the perform method check if the user is attempting to access an restricted UIViewController (perhaps restricted controllers have a protocol property that specifies the required status: logged in/out). However the pitfall here is that you cannot choose the class/subclass of a UIStoryboardSegue in the storyboard graphic editor. I am aware that I could do it programatically, however that seems tedious as i would have to add IBActions and like that to methods that perform segues, furthermore I don't think that would work with the way elements such as navigationController and tabbarControllers behave.

没有任何人有一个可行的解决方案,以限制用户的导航?

更新3 结果
我已经添加了这个问题的答案,但是我仍然认为它未答复,因为我写的答案并没有考虑到导航栏控制器之间的帐户塞格斯。但是它可以帮助一些人。

Update 3
I've added an answer to this question, however I still deem it as unanswered because the answer I've written doesn't take into account segues between navigation bar controllers. However it may help some people.

推荐答案

所以,我已经回答了如何做到这一点使用自定义塞格斯。

So, I've answered how to do this using custom segues.

现在我明白我真正的问题是与tabbarcontrollerdelegate协议精神脱节。

Now I understand my real problem was the mental disconnect with the tabbarcontrollerdelegate protocol.

作为解决preventing访问标签我做了一个类来处理表示标签栏控制器

As a solution to preventing access to tabs I've made a class to handle said tab bar controller

#import <Foundation/Foundation.h>

@interface TabBarDelegate : NSObject<UITabBarControllerDelegate,UIAlertViewDelegate>{
    UITabBarController *cachedTabBarController;
}

@end

#import "TabBarDelegate.h"

@implementation TabBarDelegate

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSLog(@"%s",__FUNCTION__);
    NSLog(@"Pretending the user isnt logged in");

    if(true){
        NSString *message = [NSString stringWithFormat:@"You require an account to access %@",viewController.tabBarItem.title];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Account Required" message:message delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: @"Login",@"Create Account",nil];
        [alert show];
        //Hold tabbarcontroller property for alert callback
        cachedTabBarController = tabBarController;
        return false;
    }

    return true;
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if(cachedTabBarController){
        switch (buttonIndex) {
            case 1:
                //Login
                [cachedTabBarController performSegueWithIdentifier:@"tabBarToLogin" sender:cachedTabBarController];
                break;
            case 2:
                //Sign up
                [cachedTabBarController performSegueWithIdentifier:@"tabBarToSignup" sender:cachedTabBarController];
                break;
            default:
                break;
        }
        //Release tab bar controller from memory
        cachedTabBarController = nil;
    }
}

@end

然后我连接起来,以我的appDelegate中的applicationDidFinishLaunching ...

Then i wired it up to my appDelegate in applicationDidFinishLaunching...

//Hook up tab bar delegate
    mainTabController = (UITabBarController*)self.window.rootViewController;
    mainTabBarDelegate = [[TabBarDelegate alloc] init];
    mainTabController.delegate = mainTabBarDelegate;

和瞧

这篇关于prevent使用带标签栏控制器UIViewControllers(分镜)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆