Objective c 如何仅在应用程序中的特定位置放置选项卡栏?

Objective c 如何仅在应用程序中的特定位置放置选项卡栏?,objective-c,cocoa-touch,uitabbarcontroller,Objective C,Cocoa Touch,Uitabbarcontroller,我想在我的应用程序中为一个位置设置一个选项卡栏。从那里我可以加载其他一些视图控制器,但仅此而已。我不想把它用于我的整个申请。我通读的所有示例都是将选项卡栏控制器作为窗口的rootviewcontroller 那我该怎么做呢? 谢谢大家! 您必须在应用程序中创建tabBar委托,然后在需要时添加 -(void)addTabBarController { AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplicat

我想在我的应用程序中为一个位置设置一个选项卡栏。从那里我可以加载其他一些视图控制器,但仅此而已。我不想把它用于我的整个申请。我通读的所有示例都是将选项卡栏控制器作为窗口的rootviewcontroller

那我该怎么做呢?
谢谢大家!

您必须在应用程序中创建tabBar委托,然后在需要时添加

-(void)addTabBarController
{
    AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];

    //[[[appdelegte navigationController] view]removeFromSuperview];

    [[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];

    [[appdelegte tabcontroller]setSelectedIndex:0];
} 

他可能会帮助你,考虑你的视图控制器是HoeVew,从那里你将推一个Tab Bar控制器,HoeVew被加载到下面的视图:

#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    HomeView *homeview = [[HomeView alloc] initWithNibName:@"HomeView" bundle:nil];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:homeview];
    self.window.rootViewController = navigation;
    [self.window makeKeyAndVisible];
    return YES;
}
@end
那么HomeView的.h、.m和.xib文件如下所示:

在HomeView.h中

#import <UIKit/UIKit.h>

@interface HomeView : UIViewController
{
    IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
@end
.xib文件必须如下所示:

tabBarController
IBOutlet
必须连接到.xib文件上的UITabBarController。还有
UITabBarController
,它有两个名为
FirstViewController
SecondViewController
的视图控制器。此外,HomeView必须位于
UINavigationController

如果没有用这个答案澄清,我会更新详细的解释。 以上是使用XIB方法加载选项卡栏控制器的一种方法

您可以通过在HomeView.m文件中更改
iAction
(按钮操作)中的某些内容来进行如下编码:

#import "HomeView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation HomeView

-(IBAction)loadTabBar:(id)sender
{
    FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
    [self.navigationController pushViewController:tabBarController animated:YES];
}
@end

那你想要什么?。只要在你想要的时候添加它,在你想要删除的时候删除它就可以了]
#import "HomeView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation HomeView

-(IBAction)loadTabBar:(id)sender
{
    FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
    [self.navigationController pushViewController:tabBarController animated:YES];
}
@end