2011-09-20 41 views
1

当我还是新在此,如此忍受我观点不改变成为一款通用应用。之后,我用相关的视图控制器编程创建了一个TabBarController(只有带有1-2个标签的基本视图来帮助我识别每个版本(iPad,iPhone)。不火,标题显示正确,但视觉观点不切换它仍然在缺少什么我在这里默认的主窗口(设备),每个版本的.xib文件的XCode 4 - - 通用应用程序中使用标签栏控制器

编程方式添加标签:?

- (void) addTabs 
{ 
//set up a local nav controller which we will reuse for each view controller 
UINavigationController *localNavigationController; 

//create tab bar controller and array to hold the view controllers 
tabBarController = [[UITabBarController alloc] init]; 
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2]; 

//setup the first view controller (Root view controller) 
HomeViewController *myViewController; 
myViewController = [[HomeViewController alloc] init]; 

//create the nav controller and add the root view controller as its first view 
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController]; 

// add the new nav controller (with the root view inside of it) 
// to the array of controllers 
[localControllersArray addObject:localNavigationController]; 

//release 
[localNavigationController release]; 
[myViewController release];  

//setup the second view controller 
MapViewController *secondViewController; 
secondViewController = [[MapViewController alloc] initWithTabBar]; 

localNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; 

[localControllersArray addObject:localNavigationController]; 
[localNavigationController release]; 
[secondViewController release]; 

tabBarController.viewControllers = localControllersArray; 
[localControllersArray release]; 

} 

and MapViewController.h is basic - there's nothing nothing there:

#import "MapViewController.h" 

@implementation MapViewController 

- (id)initWithTabBar { 
if ([self init]) { 
    //this is the label of the tab button itself 
    self.title = @"Map"; 

    //image goes here 
    //self.tabBarItem.image = [UIImage imageNamed:@"name_gray.png"]; 

    //set the long name show in the Navigation bar at the top 
    self.navigationItem.title = @"Map View"; 

} 

return self; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
    NSLog(@"Am I loading?"); 
} 


- (void)viewDidUnload 
    { 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
    } 

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
NSLog(@"Testing"); 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

现在,没有MapViewController.xib文件,而是MapViewController_iPhone.xib和MapViewController_iPad.xib文件。

+0

你有任何你可以分享的代码吗? – msgambel

+0

在这里引用自己 - 现在,没有MapViewController.xib文件,但是MapViewController_iPhone.xib和MapViewController_iPad.xib文件。 - 这可能是问题吗?如果可能的话,我想避免使用InterfaceBuilder并以编程方式执行此操作。 – javisrk

+0

他们应该被命名为:〜iphone,〜ipad,而不是*** _iPhone和_IPad。 – msgambel

回答

0

他们应该被命名为:~iphone~ipad,而不是:_iPhone和_IPad。原因是这是reference所说的格式。

另外请注意,模拟器是不是区分大小写,而iOS设备是,所以请确保您有nib格式如上,否则,它只能在模拟器上工作。

希望有助于!

相关问题