gpt4 book ai didi

iphone - 如何使 UITabBarController 延迟加载 View Controller ?

转载 作者:行者123 更新时间:2023-12-03 18:41:20 28 4
gpt4 key购买 nike

我有一个以编程方式创建的 UITabBarController,它管理 UIViewController 的 4 个子类。像这样的东西:

//Create Controller 1
self.terminal = [[[TerminalController alloc] initWithNibName:@"TerminalView" bundle:nil] autorelease];
UINavigationController* navTerminal = [[[UINavigationController alloc] initWithRootViewController:terminal] autorelease];
navTerminal.title = __(@"Terminal");
navTerminal.navigationBar.barStyle = UIBarStyleBlackOpaque;
navTerminal.tabBarItem.image = [UIImage imageNamed:@"tab_terminal.png"];

//Create Controller 2
self.history = [[[HistoryController alloc] initWithNibName:@"HistoryView" bundle:nil] autorelease];
UINavigationController* navHistory = [[[UINavigationController alloc] initWithRootViewController:history] autorelease];
navHistory.title = __(@"History");
navHistory.navigationBar.barStyle = UIBarStyleBlackOpaque;
navHistory.tabBarItem.image = [UIImage imageNamed:@"tab_history.png"];

//Create Controller 3
self.settings = [[[SettingsController alloc] initWithNibName:@"SettingsView" bundle:nil] autorelease];
UINavigationController* navSettings = [[[UINavigationController alloc] initWithRootViewController:settings] autorelease];
navSettings.title = __(@"Settings");
navSettings.navigationBar.barStyle = UIBarStyleBlackOpaque;
navSettings.tabBarItem.image = [UIImage imageNamed:@"tab_settings.png"];

//Create Controller 4
HelpController* help = [[[HelpController alloc] initWithNibName:@"HelpView" bundle:nil] autorelease];
UINavigationController* navHelp = [[[UINavigationController alloc] initWithRootViewController:help] autorelease];
navHelp.title = __(@"Help");
navHelp.navigationBar.barStyle = UIBarStyleBlackOpaque;
navHelp.tabBarItem.image = [UIImage imageNamed:@"tab_help.png"];

//Create Tab Bar an add it's view to window.
self.tabBar = [[[UITabBarController alloc] initWithNibName:nil bundle:nil] autorelease];
tabBar.viewControllers = [[[NSArray alloc] initWithObjects:navTerminal, navHistory, navSettings, navHelp, nil] autorelease];
tabBar.delegate = self;

[window addSubview:tabBar.view];

有没有办法告诉 UITabBarController 延迟加载 View Controller ? ej,当用户单击选项卡栏项目之一或调用 tabBarController setSelectedIndex 时?

最佳答案

我正在我的一个应用程序中做这件事。诀窍是让你的 View Controller 不是 UITabBarController 的子类,而是 UIViewController,并实现 UITabBarDelegate。我在 IB 中为此创建了 View ,布置了选项卡栏(带有正确的按钮、图像、标签等数量)和占位符 UIView,用于正确放置换入和换出的 subview 。 View 切换发生在 tabBar:didSelectItem: 看起来像这样:

// MyTabBarController.h
@class MyFirstViewController;
@class MySecondViewController;
@class MyThirdViewController;

@interface MyTabBarController : UIViewController <UITabBarDelegate> {
IBOutlet UIView *placeholderView;
IBOutlet UITabBar *tabBar;
MyFirstViewController *firstViewController;
MySecondViewController *secondViewController;
MyThirdViewController *thirdViewController;
UIViewController *currentViewController;
}
@property (nonatomic, retain) MyFirstViewController *firstViewController;
@property (nonatomic, retain) MySecondViewController *secondViewController;
@property (nonatomic, retain) MyThirdViewController *thirdViewController;

- (void) switchToView:(UIViewController*)aViewController;
@end


// MyTabBarController.m
#import "MyTabBarController.h"
#import "MyFirstViewController.h"
#import "MySecondViewController.h"
#import "MyThirdViewController.h"

enum {
kView_First = 1,
kView_Second,
kView_Third
};

@implementation MyTabBarController

@synthesize firstViewController, secondViewController, thirdViewController;

- (void) viewDidLoad {
// Default to first view.
tabBar.selectedItem = [tabBar.items objectAtIndex:0];
MyFirstViewController *viewController = [[MyFirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
self.firstViewController = viewController;
[viewController release];
[self switchToView:firstViewController];
}

- (void)viewWillAppear:(BOOL)animated {
// Tell our subview.
if( currentViewController != nil ) {
[currentViewController viewWillAppear:animated];
}
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case kView_First: {
if (firstViewController == nil) {
MyFirstViewController *viewController = [[MyFirstViewController alloc]
initWithNibName:@"FirstView" bundle:nil];
self.firstViewController = viewController;
[viewController release];
}

[self switchToView:firstViewController];
}
break;

case kView_Second:
if (secondViewController == nil) {
MySecondViewController *viewController = [[MySecondViewController alloc]
initWithNibName:@"SecondView" bundle:nil];
self.secondViewController = viewController;
[viewController release];
}

[self switchToView:secondViewController];
break;

case kView_Third: {
if (timesViewController == nil) {
MyThirdViewController *viewController = [[MyThirdViewController alloc]
initWithNibName:@"ThirdView" bundle:nil];
self.thirdViewController = viewController;
[viewController release];
}

[self switchToView:thirdViewController];
}
break;
}
}

- (void) switchToView:(UIViewController*)aViewController {
if( aViewController == currentViewController ) return;

UIView *aView= aViewController.view;
[aViewController viewWillAppear:NO];
if( currentViewController != nil ) {
[currentViewController viewWillDisappear:NO];
[currentViewController.view removeFromSuperview];
}
aView.frame = placeholderView.frame;
[self.view insertSubview:aView aboveSubview:placeholderView];
if( currentViewController != nil ) {
[currentViewController viewDidDisappear:NO];
}
[aViewController viewDidAppear:NO];
currentViewController = aViewController;
}
@end

代码肯定可以变得更加干燥,但你明白了。

关于iphone - 如何使 UITabBarController 延迟加载 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1197130/

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