将各个视图添加到选项卡的最佳方法

时间:2014-02-06 20:28:32

标签: ios iphone objective-c view tabs

我是初学者ios程序员并且已经构建了一个不使用故事板的应用程序。不同的视图有不同的笔尖。它还有一个委托文件。

如果我想从一个视图切换到另一个视图我在viewcontroller中使用此代码。

self.detailController = [[[DetailController alloc]
                       initWithNibName:@"DetailController" bundle:nil] autorelease];
self.detailController.originalObj = nil;

SUP101AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:self.detailController animated:YES];

该应用程序在委托中具有以下代码以显示初始视图

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[SubscribeController alloc] initWithNibName:@"SubscribeController" bundle:nil] autorelease];
    self.navController = [[[UINavigationController alloc] initWithRootViewController: self.viewController] autorelease];
    self.window.rootViewController =  self.navController;
    [self.window makeKeyAndVisible];
    return YES;

}

我想更改应用程序,以便每个视图都显示在一个选项卡中,并且从一个视图切换到另一个视图应该在选项卡中进行。我尝试了其他帖子中的几个示例,但我无法逐步解决如何为现有应用程序实现基于标签的视图。我不确定是否应该在xcode中使用新文件添加选项卡控制器,或者在视图控制器中以编程方式制作选项卡。或者我应该从基于选项卡的新项目开始并将我的旧项目文件复制到其中...请建议。 / p>

1 个答案:

答案 0 :(得分:1)

这是您修改后的代码,用于启动带有4个选项卡的tabbar控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];

UIViewController *VC1=[[UIViewController alloc] initWithNibName:nil bundle:nil];//set you nib name
VC1.title=@"VC1";
UINavigationController *NC1=[[UINavigationController alloc] initWithRootViewController:VC1];//set root of navigation controller
UIViewController *VC2=[[UIViewController alloc] initWithNibName:nil bundle:nil];//set you nib name
VC2.title=@"VC2";
UINavigationController *NC2=[[UINavigationController alloc] initWithRootViewController:VC2];//set root of navigation controller
UIViewController *VC3=[[UIViewController alloc] initWithNibName:nil bundle:nil];//set you nib name
VC3.title=@"VC3";
UINavigationController *NC3=[[UINavigationController alloc] initWithRootViewController:VC3];//set root of navigation controller
UIViewController *VC4=[[UIViewController alloc] initWithNibName:nil bundle:nil];//set you nib name
VC4.title=@"VC4";
UINavigationController *NC4=[[UINavigationController alloc] initWithRootViewController:VC4];//set root of navigation controller

UITabBarController *tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:@[NC1,NC2,NC3,NC4]];

[self.window setRootViewController:tabBarController];

[self.window makeKeyAndVisible];
return YES;
}