【问题标题】:how to change the rootviewcontroller如何更改根视图控制器
【发布时间】:2013-07-31 01:17:13
【问题描述】:

我想在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];
    }
}

此代码是当我按下登录按钮时,我希望 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;
}

【问题讨论】:

    标签: iphone ios ios5


    【解决方案1】:

    您可以尝试 UINavigationController 的以下方法,其中包含一组新的所需视图控制器,例如

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

    它会做的伎俩;)

    【讨论】:

      【解决方案2】:

      在导航和身份验证视图控制器之上有一个视图控制器怎么样?这个视图控制器可以检查一个有效的会话并将任何适合的视图推送到堆栈顶部。尝试更改根视图控制器似乎是不明智的。

      编辑:更多细节

      你有一个根视图控制器,它没有像大多数其他视图控制器那样绑定视图。您将此类设置为应用委托中的根视图控制器。

      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;
      }
      

      在你的根视图控制器中,你可以确定你是否有一个有效的会话。基于此,您可以渲染适当的视图。

      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
      

      这很粗略,但它应该为您指明正确的方向。

      【讨论】:

      • 你能解释一下吗?
      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多