基本上,我在MainWindow.xib中有两个视图控制器,可以通过单击导航控制器中的栏按钮来查看。我希望这两个视图控制器与MainWindow.xib分离,它们有自己的头,实现和xib文件,并且仍然使MainWindow.xib中的导航控制器在其中工作。
为了更好地理解它,请参阅以下代码:
非常感谢!
TestAppDelegate.h
#import <UIKit/UIKit.h>
@interface TestAppDelegate : NSObject <UIApplicationDelegate>
{
//Navigation Controller
IBOutlet UINavigationController *navigationController;
//View Controllers
UIViewController *viewController;
UIViewController *viewController2;
UIViewController *viewController3;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
//Navigation Controller
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
//View Controllers
@property (nonatomic, retain) IBOutlet UIViewController *viewController;
@property (nonatomic, retain) IBOutlet UIViewController *viewController2;
@property (nonatomic, retain) IBOutlet UIViewController *viewController3;
- (IBAction)next;
- (IBAction)next2;
@end
TestAppDelegate.m
#import "TestAppDelegate.h"
@implementation TestAppDelegate
@synthesize window = _window;
//Navigation Controller
@synthesize navigationController;
//View Controllers
@synthesize viewController;
@synthesize viewController2;
@synthesize viewController3;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//Navigation Controller
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[viewController release];
[viewController2 release];
[viewController3 release];
[navigationController release];
[_window release];
[super dealloc];
}
- (IBAction)next {
[navigationController pushViewController:viewController2 animated:YES];
}
- (IBAction)next2 {
[navigationController pushViewController:viewController3 animated:YES];
}
@end
在MainWindow.xib内:
答案 0 :(得分:1)
将Interface Builder中的“class”属性(检查器中的第三个选项卡,“ Custom Class ”)设置为您计划使用的自定义类的名称,然后输入名称要在“NIB名称”(第四个选项卡)中加载的.xib文件。
用于推送viewController的代码没问题。确保永远不会意外地释放两个UIViewController中的任何一个。
说到这一点,请记住,这种方法使ViewControllers始终保持在内存中,即使在不使用时也是如此。另一种方法是完全删除两个ViewControllers的IBOutlets,并执行以下操作:
- (IBAction)next
{
MyCustomViewController *customViewController = [[MyCustomViewController alloc] initWithNibName:<#NibName or nil#> bundle:[NSBundle mainBundle]];
[navigationController pushViewController:customViewController animated:YES];
[customViewController release];
}
这会在需要时创建对象(如果我没记错的话,xib中的UI元素会被缓存到某处,因此可能无关紧要[需要引证])。根据两个ViewControllers的使用频率,记住一些事项。
答案 1 :(得分:1)
我通常不会触碰MainWindow.xib
。我建议如下:
通过转到MainController
创建一个UIViewController
,它将是您的MainView子类File > New > New File
。这将为每个ViewController创建一个.h / .m和nib文件。添加您想要的应用程序UI。例如,添加两个按钮并将这些按钮连接到MainController中的IBActions
。这应该分别在MainController.{h/m}
中声明和实施。
之后再创建另外两个ViewControllers,方法相同。
这些IBAction的主体应该创建一个ViewControllers的实例,然后推送它们。
它看起来像这样:
YourViewController *yvc = [[YourViewController alloc] init];
[self.navigationController pushViewController:yvc animated:YES];
[yvc release];
最后,您必须在AppDelegate
中推送MainController并将NavigationController
添加到视图中。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *mvc = [[MainViewController alloc] init];
UINavigationController *unvc = [[UINavigationController alloc] init];
[unvc pushViewController:mvc animated:NO];
[mvc release];
[self.window addSubview:unvc.view];
[self.window makeKeyAndVisible];
return YES;
}