简体   繁体   English

如何更改rootviewcontroller

[英]how to change the rootviewcontroller

I want to change the rootViewController after the authenticationViewController 我想在authenticationViewController之后更改rootViewController 在此处输入图片说明

-(IBAction)LoginButtonPushed:(id)sender {
    if ([(VerifyId)isEqual:@"C"]){
        CondidatsViewController *condidatsViewController = [[[CondidatsViewController alloc]initWithNibName:@"CondidatsViewController" bundle:nil]autorelease];
        UINavigationController *navController = self.navigationController;

        NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
        [controllers removeLastObject];
        navController.viewControllers = controllers;
        [navController pushViewController:condidatsViewController animated: YES];

    } else {
        RecruteursViewController *recruteursViewController = [[[RecruteursViewController alloc]initWithNibName:@"RecruteursViewController" bundle:nil]autorelease];
        UINavigationController *navController = self.navigationController;

        NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
        [controllers removeLastObject];
        navController.viewControllers = controllers;
        [navController pushViewController:recruteursViewController animated: YES];
    }
}

this code is when i press in login button i want CondidatsViewController or RecruteursViewController will be the rootView 这段代码是当我按下登录按钮时,我希望CondidatsViewController或RecruteursViewController将成为rootView

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    AcceuilViewController *viewController =[[[AcceuilViewController alloc]
                                             initWithNibName:@"AcceuilViewController" bundle:nil]autorelease];
    self.navController = [[[UINavigationController alloc]initWithRootViewController:viewController]
                         autorelease];
   self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}

You can try UINavigationController's following method with a new array of desired view controllers something like 您可以使用新的所需视图控制器数组尝试UINavigationController的以下方法,例如

    [self.navigationController setViewControllers:@[newRootViewControllerInstance, secondVCInstanceIfRequired, thirdVC-and-so-on....] animated:NO];

It will do the trick ;) 它将解决问题;)

How about have a view controller one level above your nav and auth view controllers? 在导航和认证视图控制器之上的视图控制器又如何呢? This view controller can check for a valid session and push whatever view is appropriate on top of the stack. 该视图控制器可以检查有效的会话并将任何合适的视图推入堆栈顶部。 It seems ill advised to try to change your root view controller. 似乎不建议尝试更改您的根视图控制器。

Edit: More details 编辑:更多详细信息

You have a root view controller that doesn't have a view tied to it like most other view controllers might. 您有一个根视图控制器,该视图控制器没有像大多数其他视图控制器那样绑定到一个视图。 You set this class as the root view controller in your app delegate. 您可以在应用程序委托中将此类设置为根视图控制器。

App Delegate.m App Delegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    RootViewController *rootViewController = [[RootViewController alloc] init];

    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

In your root view controller you can determine if you have a valid session or not. 在根视图控制器中,您可以确定是否具有有效的会话。 Based on that, you can render the appropriate view. 基于此,您可以渲染适当的视图。

RootViewController.m RootViewController.m

@interface RootViewController ()
@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    BOOL isUserAuthenticated = [MyClasskToFigureOutIfUserIsAuthenticated isUserAuthenticated];

    if(isUserAuthenticated == NO) {
        AuthViewController *vcAuth = [[AuthViewController alloc] init];
        [self addChildViewController:vcAuth];
        [self.view addSubView:vcAuth.view];
    } else {
        //they are authenticated so push your other view controller.
    }
}

@end

This is pretty rough, but it should point you in the right direction. 这是很粗糙的,但是应该为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM