1

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

在我的 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;” 是罪魁祸首。任何人都可以指导我。

4

1 回答 1

1

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

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

进而

self.window.rootViewController = navController;

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

于 2012-09-19T16:54:28.993 回答