代码之家  ›  专栏  ›  技术社区  ›  shailbenq

由于AppDelegate方法导致切换视图时出错

  •  1
  • shailbenq  · 技术社区  · 14 年前

    这就是我在应用程序中所做的:

    在我的appDelegate.m文件中,

    //Four Views
    @synthesize fvc;
    @synthesize svc;
    @synthesize tvc;
    @synthesize pvc;
    
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
     {
     NSString *name1,*name2,*name3;
     name1 = NSLocalizedString(@"home", nil);
     name2 = NSLocalizedString(@"quote", nil);
    name3 = NSLocalizedString(@"ship", nil);
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
    
     //[application setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    tvc = [storyboard instantiateViewControllerWithIdentifier:@"thirdview"];
    fvc = [storyboard instantiateViewControllerWithIdentifier:@"firstview"];
    svc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    
     pvc =[storyboard instantiateViewControllerWithIdentifier:@"PayViewController"];
    
    fvc.tabBarItem.title = name1;
     fvc.tabBarItem.image = [UIImage imageNamed:@"home.png"];
     fvc.tabBarItem.titlePositionAdjustment = UIOffsetMake(2.0, 0);
    
     tvc.tabBarItem.title = name2;
     tvc.tabBarItem.image = [UIImage imageNamed:@"ping.png"];
     tvc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 2.0);
    
     svc.tabBarItem.title = name3;
     svc.tabBarItem.image = [UIImage imageNamed:@"zoom.png"];    
     svc.tabBarItem.titlePositionAdjustment = UIOffsetMake(-5.0, 0);
    
     NSLog(@"appdelegate %@ and %@",svc,svc.title);
    
     tabBar.viewControllers = [NSArray arrayWithObjects:fvc,svc,tvc,nil];
    
     // the below line does not allow [self.navigationController pushViewController:svNew  animated:YES]; to function
    
     self.window.rootViewController = tabBar;
     // Override point for customization after application launch.
     return YES;
    

    在我的fvc视图中,我正在okPressed方法中执行更改视图操作:

     - (IBAction)okPressed:(id)sender {
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
    
     SecondViewController *svNew = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    
     [self.navigationController pushViewController:svNew animated:YES];
     }
    

    当我在appdelegate方法中注释代码时,通过okPressed方法在视图之间的切换将成功执行。但是,当我取消注释appDelegate方法时,它不会执行切换。我发现,appdelegate方法“self.window.rootViewController=tabBar;”中的代码是罪魁祸首。有人能指导我吗。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Alejandro Benito-Santos    14 年前

    我认为您应该将UITabBarController嵌入到导航控制器中以使其工作:

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tabBar];
    

    然后

    self.window.rootViewController = navController;
    

    否则你就不能推送任何东西,因为你可能没有UINavigationController可以推送东西!

    推荐文章