SplitViewController 以登录视图控制器为根 [英] SplitViewController with a Login View Controller as the root

查看:70
本文介绍了SplitViewController 以登录视图控制器为根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在研究如何将登录视图控制器作为初始视图控制器而不是拆分视图.

我看到的一些答案会推荐加载模态视图?我不确定它是如何设置的.

例如

如何在 UISplitViewController 之前添加登录视图iPad

如何在二级实现SplitViewController.?>

那么我是否将这些添加到 loginviewcontroller 类中?或者在哪里?

欢迎任何建议.

谢谢!!

解决方案

我通过创建两个故事板来完成此任务:一个使用(全屏)登录,一个使用拆分视图.

为了在它们之间切换,我添加了一个自定义协议:

#import @protocol RootViewControllerDelegate -(void)switchToStoryboard: (UIStoryboard *) storyboard animationDirectionOrNil: (NSString *)direction;@结尾

AppDelegate 然后实现这个协议:

-(void)switchToStoryboard:(id)storyboard animationDirectionOrNil:(NSString *)direction {UIViewController *newRoot=[storyboad instantiateInitialViewController];if ([newRoot RespondsToSelector:@selector(setRootViewControllerDelegate:)]) {[newRoot setRootViewControllerDelegate:self];}self.window.rootViewController=newRoot;如果(方向){CATransition* transition=[CATransition 动画];transition.type=kCATransitionPush;过渡.子类型=方向;[self.window.layer addAnimation:transition forKey:@"push_transition"];}}

如您所见,它再次尝试将自己设置为委托,以便其他视图控制器可以切换回或切换到另一个故事板.为了让它工作,你必须继承 UISplitView:

标题

#import #import "RootViewControllerDelegate.h"@interface MySplitViewController : UISplitViewController@property (nonatomic, weak) id rootViewControllerDelegate;@结尾

实施

#import "MySplitViewController.h"@implementation MySplitViewController@synthesize rootViewControllerDelegate;- (void)viewDidLoad{[超级viewDidLoad];for (UIViewController *viewController in self.viewControllers) {if ([viewController RespondsToSelector:@selector(setRootViewControllerDelegate:)]) {[viewController setRootViewControllerDelegate:self.rootViewControllerDelegate];}}}@结尾

这个简单的实现寻找接受根视图控制器委托并传递给它的子视图控制器.因此,当您想将显示登录"按钮添加到某个(主视图或详细信息)视图时,只需创建您自己的 UIViewController 子类,添加一个 @property id;rootViewControllerDelegate 并将这样的操作与按钮相关联:

- (IBAction)loginButtonClicked:(id)sender {UIStoryboard *mainSB=[UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];NSString *animationDirection=kCATransitionFromTop;UIDeviceOrientation currentOrientation=[[UIDevice currentDevice] 方向];如果(currentOrientation==UIDeviceOrientationLandscapeLeft){animationDirection=kCATransitionFromBottom;}[self.rootViewControllerDelegate switchToStoryboard:mainSB animationDirectionOrNil:animationDirection];}

您可以根据自己的需要随意调整一切.

So I've been researching on how to make a login view controller as the initial view controller instead of the splitview.

Some of the answers I've seen would recommend a modal view to be loaded? I'm not sure how that is set up.

eg.

How to add a login view before a UISplitViewController iPad

and

How to implement SplitViewController on second level.?

So do I add those on the loginviewcontroller class? Or where?

Any advice is welcome.

Thanks!!

解决方案

I've done this by creating two storyboards: one with the (full-screen) login and one with the split-view.

To switch between them, I've added a custom protocol:

#import <Foundation/Foundation.h>

@protocol RootViewControllerDelegate <NSObject>

-(void)switchToStoryboard: (UIStoryboard *) storyboad animationDirectionOrNil: (NSString *)direction;

@end

The AppDelegate then implements this protocol:

-(void)switchToStoryboard:(id)storyboad animationDirectionOrNil:(NSString *)direction {
    UIViewController *newRoot=[storyboad instantiateInitialViewController];
    if ([newRoot respondsToSelector:@selector(setRootViewControllerDelegate:)]) {
        [newRoot setRootViewControllerDelegate:self];
    }
    self.window.rootViewController=newRoot;

    if(direction){
        CATransition* transition=[CATransition animation];
        transition.type=kCATransitionPush;
        transition.subtype=direction;
        [self.window.layer addAnimation:transition forKey:@"push_transition"];
    }
}

As you can see, it tries to set itself as the delegate again, so the other view-controller can switch back or to another storyboard. In order for this to work, you would have to subclass UISplitView:

Header

#import <UIKit/UIKit.h>
#import "RootViewControllerDelegate.h"

@interface MySplitViewController : UISplitViewController
@property (nonatomic, weak) id <RootViewControllerDelegate> rootViewControllerDelegate;

@end

iMplementation

#import "MySplitViewController.h"

@implementation MySplitViewController
@synthesize rootViewControllerDelegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    for (UIViewController *viewController in self.viewControllers) {
        if ([viewController respondsToSelector:@selector(setRootViewControllerDelegate:)]) {
            [viewController setRootViewControllerDelegate:self.rootViewControllerDelegate];
        }
    }
}

@end

This simple implementation looks for child-view-controllers that accept a root-view-controller-delegate and hands it down. So when you want to add a "Show Login"-button to a certain (master- or detail-)view, just create your own UIViewController-subclass, add a @property id<RootViewControllerDelegate> rootViewControllerDelegate and associate an action like this with the button:

- (IBAction)loginButtonClicked:(id)sender {
    UIStoryboard *mainSB=[UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
    NSString *animationDirection=kCATransitionFromTop;
    UIDeviceOrientation currentOrientation=[[UIDevice currentDevice] orientation];
    if (currentOrientation==UIDeviceOrientationLandscapeLeft) {
        animationDirection=kCATransitionFromBottom;
    }
    [self.rootViewControllerDelegate switchToStoryboard:mainSB animationDirectionOrNil:animationDirection];
}

Feel free to adjust everything to your needs.

这篇关于SplitViewController 以登录视图控制器为根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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