我在基于 UINavigation 的应用程序中添加了一个标签栏,并且“applicationWillTerminate”(见下文)中的代码不再有效。我试图改变代码以适应标签栏,但无法让它工作。
如何更改“applicationWillTerminate”中的代码以使用我的标签栏?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[window makeKeyAndVisible];
[self createTabBar];
// this subview causes the code in 'applicationWillTerminate' to no longer work
[window addSubview:tabBarController.view];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *viewController = (RootViewController *)[navigationController.viewControllers objectAtIndex:0];
[viewController.dataOjects saveData];
}
最佳答案
[self createTabBar]
是什么意思究竟是做什么的?如果您在那里以编程方式创建 RootViewController,您不妨只创建一个 @property (nonatomic, retain) RootViewController *myRootController;
在您的应用程序委托(delegate)类中,将创建的 Controller 保存在正在创建的任何位置(最坏的情况:RootViewController 在 viewDidLoad
中创建后将自身保存到应用程序委托(delegate))并使用 self.myRootController
而不是通过 View 层次结构。
这样,您就可以独立于 View 的实际组成,如果您正在浏览 UITabbar 等的 subview ,甚至可能会在 iOS 更新上发生变化。
关于iphone - 一起使用 UINavigationController 和标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8145981/