我正在学习Objective-C,在学习它的同时,我开始创建一个App。我已经创建了一个带有两个名为"ViewController“和"secondViewController”的“视图控制器”的“单视图应用程序”,之后我想添加一个"UITabBarController",因此在本教程之后,我在AppDelegate.m中使用了以下代码。
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeInterface];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];
[viewController1 setTabBarItem:tab1];
UIViewController *viewController2 = [[UIViewController alloc] init];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Music" image:[UIImage imageNamed:@"music-tab.png"] tag:2];
[viewController2 setTabBarItem:tab2];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
这里是ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如果我运行应用程序,选项卡栏可以工作,但我有一个空白屏幕,而不是我创建的ViewController。因为我是菜鸟,你能告诉我如何在每个选项卡中分别链接或显示视图控制器吗?
请亲切地说出所有的事情,以及显而易见的事情。提前谢谢你
发布于 2013-02-01 01:41:10
由于你是初学者,我强烈建议你使用故事板,这将有助于更容易地管理界面,因为你几乎不需要编写任何代码。
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
我只是猜测,但你的问题可能是你直接init
你的视图控制器,你应该在你的didFinishLaunchingWithOptions
中调用initWithNibName:
方法。initWithNibName:
是当您从nib文件创建控制器实例时调用的东西。
因此,initWithNibName:
基本上是在加载和实例化NIB时调用的。
我不知道你的ViewContoller类名,但是你应该改变你的视图控制器初始化方法
在你的应用delegate.m中
#import "ViewController.h"
#import "secondViewController.h"
UIViewController *viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]]; // make sure on interface builder hat you Nib name is ViewController
UIViewController *viewController2 = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:[NSBundle mainBundle]];// make sure on interface builder hat you Nib name is secondViewController
并且请使用大写字母和详细名称作为类名secondViewController不是一个好的编程实践
发布于 2013-02-01 01:45:22
选项卡栏控制器显示了您在代码中创建的视图。这是预期的行为,因为您正在创建全新的视图。
如果您在Interface Builder中创建了"ViewController“和"secondViewController”,那么您必须加载nibs而不是创建新视图-并告诉to栏控制器使用它们。
基本上,您必须更改您的代码行:
UIViewController *viewController1 = [[UIViewController alloc] init];
类似这样的东西
UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"view1" bundle:nil];
详情请参考initWithNibName:bundle:
。
https://stackoverflow.com/questions/14631527
复制相似问题