【发布时间】:2011-09-20 21:06:05
【问题描述】:
我还是新手,所以请耐心等待:
我在 XCode 4 中创建了一个新的基于 Window 的应用程序,它恰好是一个通用应用程序。之后,我以编程方式创建了一个带有关联视图控制器的 TabBarController(只是带有 1-2 个标签的基本视图,以帮助我识别每个版本(iPad、iPhone)。
当我运行应用程序并切换选项卡时,viewDidLoad 方法会触发,标题正确显示,但视觉视图没有切换。它保留在每个版本的默认 MainWindow(device).xib 文件中。我在这里错过了什么?
以编程方式添加选项卡:
- (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];
}
而且 MapViewController.h 是基本的 - 那里真的什么都没有:
#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 文件。
【问题讨论】:
-
你有什么代码可以分享吗?
-
在这里引用我自己的话 - 现在,没有 MapViewController.xib 文件,但是 MapViewController_iPhone.xib 和 MapViewController_iPad.xib 文件。 - 这可能是问题所在?如果可能的话,我想避免使用 InterfaceBuilder 并以编程方式执行此操作。
-
它们应该被命名为:~iphone, and ~ipad, and not _iPhone and _iPad.
-
所以它有效,应该作为答案发布。但是为什么是波浪号而不是下划线呢?只是想在这里理解。