gpt4 book ai didi

iphone - XCode 4 - 通用应用程序 - 使用选项卡栏 Controller 时 View 不会改变

转载 作者:行者123 更新时间:2023-11-28 17:42:51 25 4
gpt4 key购买 nike

我在这方面还是个新手,所以请多多包涵:

我在 XCode 4 中创建了一个新的基于 Window 的应用程序,它恰好是一个通用应用程序。之后,我以编程方式创建了一个带有相关 View Controller 的 TabBarController(只是带有 1-2 个标签的基本 View ,以帮助我识别每个版本(iPad、iPhone)。

当我运行应用程序并切换选项卡时,viewDidLoad 方法会触发,标题正确显示,但视觉 View 不会切换。它保留在每个版本的默认 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 文件。

最佳答案

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

另请注意,模拟器区分大小写,而 iOS 设备则区分大小写,因此请确保您的 nib 格式如上,否则,它只能在模拟器上运行。

希望对您有所帮助!

关于iphone - XCode 4 - 通用应用程序 - 使用选项卡栏 Controller 时 View 不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7491775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com