我想在我的应用程序的一个地方放置一个标签栏。从那里我可以加载一些其他的视图控制器,但就是这样。我不希望它用于我的整个应用程序。我阅读的所有示例都将标签栏控制器作为窗口的根视图控制器。
那么我该怎么做呢?谢谢!
我想在我的应用程序的一个地方放置一个标签栏。从那里我可以加载一些其他的视图控制器,但就是这样。我不希望它用于我的整个应用程序。我阅读的所有示例都将标签栏控制器作为窗口的根视图控制器。
那么我该怎么做呢?谢谢!
他可能会对您有所帮助,考虑您的视图控制器是 HomeView,您将从那里推送一个标签栏控制器,HomeView 在下面的视图中加载:
#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
HomeView.m 是:
@implementation HomeView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)loadTabBar:(id)sender
{
[self.navigationController pushViewController:tabBarController animated:YES];
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
并且 .xib 文件必须如下:
tabBarControllerIBOutlet
必须连接到 .xib 文件上的 UITabBarController。还有UITabBarController
两个名为FirstViewController
,的视图控制器SecondViewController
。此外, HomeView 必须在UINavigationController
.
如果没有用这个答案澄清,我会更新详细解释。以上是使用XIB方法加载标签栏控制器的一种方式。
IBAction
您可以通过在 HomeView.m 文件中更改 (button action) 中的内容来编写如下代码:
#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
您必须在应用程序委托中制作 tabBar ,然后在您想使用它时添加
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
//[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}