如何将多个自定义视图控制器与导航控制器一起使用?
基本上,我在 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 内部:
Basically, I have two view controllers inside the MainWindow.xib that can be viewed by clicking the Bar Button in my Navigation Controller. I wanted those two view controllers to be separated from the MainWindow.xib with their own header, implementation and xib files and still make Navigation Controller inside of MainWindow.xib work in them.
To better understand it, please see the codes below:
Thanks a lot!
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
Inside MainWindow.xib:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常不会碰
MainWindow.xib
。我建议如下:创建一个
MainController
,它将成为您的MainView,它是UIViewController
的子类,方法是转到File > >新的>新文件
。这将为每个 ViewController 创建一个 .h/.m 和 nib 文件。为您的应用程序添加您想要的任何 UI。例如,添加两个按钮并将这些按钮连接到 MainController 中的IBActions
。这应该分别在您的MainController.{h/m}
中声明和实现。然后以同样的方式创建另外两个ViewController。
这些 IBActions 的主体应该创建 ViewController 的实例,然后推送它们。
它看起来像这样:
最后,您必须将 MainController 推送到 AppDelegate 中,并将
NavigationController
添加到视图中。I normally don't touch
MainWindow.xib
. I suggest the following:Create a
MainController
which will be your MainView that subclassUIViewController
by going toFile > New > New File
. That will create a .h/.m and nib file for each ViewController. There add whatever UI you want for your app. For Example, add two buttons and wire those buttons toIBActions
in your MainController. This should be declared and implemented in yourMainController.{h/m}
, respectively.After that create another two ViewControllers, the same way.
The body of those IBActions should create an instance of the your ViewControllers and then push them.
It would look something like this:
Finally, you have to push the MainController in your
AppDelegate
and add yourNavigationController
to the view.将 Interface Builder 中的“class”属性(检查器中的第三个选项卡,“自定义类”)设置为您计划使用的自定义类的名称,然后将 .您要从“NIB 名称”(第四个选项卡)中加载的 xib 文件。
您用来推送 viewController 的代码没问题。确保永远不会意外释放两个 UIViewController 中的任何一个。
说到这里,请记住,这种方法将 ViewController 始终保留在内存中,即使在不使用时也是如此。另一种方法是完全删除两个 ViewController 的 IBOutlet,并执行以下操作:
这会在需要时创建对象(如果我没记错的话,xib 中的 UI 元素会缓存在某处,因此这可能是不相关的[需要引用]) 。只是需要记住一些事情,具体取决于两个 ViewController 的使用频率。
Set the "class" property in Interface Builder (third tab in the inspector, "Custom Class") to the name of the custom class you're planning to use, and then put the name of the .xib file you want to load from in the "NIB Name" (fourth tab).
The code you're using to push the viewController is alright. Make sure to never accidentaly dealloc any of the two UIViewController.
Speaking of which, keep in mind that this approach is keeping the ViewControllers always in memory, even when not in use. Another approach is to completely remove the IBOutlets for the two ViewControllers, and do something like:
This creates the object when needed (If I remember right, the UI elements from the xib are cached somewhere, so that may be irrelevant[citation needed]). Just something to keep in mind, depending on the frequency of use of your two ViewControllers.