如何将多个自定义视图控制器与导航控制器一起使用?

发布于 2024-12-03 08:44:25 字数 2581 浏览 0 评论 0原文

基本上,我在 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:

http://i52.tinypic.com/10xa45f.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-12-10 08:44:26

我通常不会碰 MainWindow.xib。我建议如下:

  1. 创建一个MainController,它将成为您的MainView,它是UIViewController的子类,方法是转到File > >新的>新文件。这将为每个 ViewController 创建一个 .h/.m 和 nib 文件。为您的应用程序添加您想要的任何 UI。例如,添加两个按钮并将这些按钮连接到 MainController 中的 IBActions。这应该分别在您的 MainController.{h/m} 中声明和实现。

  2. 然后以同样的方式创建另外两个ViewController。

  3. 这些 IBActions 的主体应该创建 ViewController 的实例,然后推送它们。

它看起来像这样:

YourViewController *yvc = [[YourViewController alloc] init];
[self.navigationController pushViewController:yvc animated:YES];
[yvc release];

最后,您必须将 MainController 推送到 AppDelegate 中,并将 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;
 }

I normally don't touch MainWindow.xib. I suggest the following:

  1. Create a MainController which will be your MainView that subclass UIViewController by going to File > 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 to IBActions in your MainController. This should be declared and implemented in your MainController.{h/m}, respectively.

  2. After that create another two ViewControllers, the same way.

  3. The body of those IBActions should create an instance of the your ViewControllers and then push them.

It would look something like this:

YourViewController *yvc = [[YourViewController alloc] init];
[self.navigationController pushViewController:yvc animated:YES];
[yvc release];

Finally, you have to push the MainController in your AppDelegate and add your NavigationController to the view.

- (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;
 }
南七夏 2024-12-10 08:44:26

将 Interface Builder 中的“class”属性(检查器中的第三个选项卡,“自定义类”)设置为您计划使用的自定义类的名称,然后将 .您要从“NIB 名称”(第四个选项卡)中加载的 xib 文件。

您用来推送 viewController 的代码没问题。确保永远不会意外释放两个 UIViewController 中的任何一个。

说到这里,请记住,这种方法将 ViewController 始终保留在内存中,即使在不使用时也是如此。另一种方法是完全删除两个 ViewController 的 IBOutlet,并执行以下操作:

- (IBAction)next
{
    MyCustomViewController *customViewController = [[MyCustomViewController alloc] initWithNibName:<#NibName or nil#> bundle:[NSBundle mainBundle]]; 
    [navigationController pushViewController:customViewController animated:YES];
    [customViewController release];
}

这会在需要时创建对象(如果我没记错的话,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:

- (IBAction)next
{
    MyCustomViewController *customViewController = [[MyCustomViewController alloc] initWithNibName:<#NibName or nil#> bundle:[NSBundle mainBundle]]; 
    [navigationController pushViewController:customViewController animated:YES];
    [customViewController release];
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文