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

时间:2012-09-18 01:56:23

标签: ipad ios5 uiview

这就是我在申请中所做的事情:

在我的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 个答案:

答案 0 :(得分:1)

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

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

然后

self.window.rootViewController = navController;

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