ios如何使用多个自定义的视图控制器与Navigation Controller?

发布时间: 2017/3/27 19:59:55
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

基本上,我有两个视图控制器里面可以查看通过单击任务栏按钮在我Navigation Controller MainWindow.xib。我想要那些两个视图控制器要分开自己头、 执行与 xib 文件 MainWindow.xib,并仍在 MainWindow.xib 工作在他们使Navigation Controller。

要更好地理解它,请参见下面的代码︰

多谢!

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:

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

解决方法 1:

我通常不要touch MainWindow.xib 。我提出以下建议︰

  1. 创建 MainController ,将您的 MainView 该子类 UIViewControllerFile > New > New File 。这将为每个 ViewController 创建一个.h/.m 和 nib 文件。没有添加任何你想要为您的应用程序的 UI。例如,添加两个按钮和钢丝到这些按钮 IBActions 在你 MainController。这应该宣布并执行在你 MainController.{h/m} ,分别。

  2. 在那之后创建另一个的两个 ViewControllers,以同样的方式。

  3. 这些 IBActions 的身体应该创建实例的你的 ViewControllers,就能推动他们。

它会看起来像这样︰

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;
 }
赞助商