0

我是新手。ViewController.m 中有一个按钮。当我按下按钮时,它必须导航到 SecondViewController 但 SecondViewController 没有出现。

并且在导航栏的SecondViewController处,会有“Back”按钮返回ViewController。你能告诉我我错过了什么吗?

视图控制器.m:

-(IBAction)buttonPressed:(id)sender
{

    EnglishViewController *v = [[[EnglishViewController alloc]
                                initWithNibName:@"EnglishViewController"
                                bundle:nil]
                               autorelease];


    [self.navigationController pushViewController:v animated:TRUE];
}

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

解决方案(取决于@George 的回答):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    UINavigationController *navCon =[[[UINavigationController alloc]
                                      initWithRootViewController:self.viewController]
                                     autorelease];

    self.window.rootViewController =navCon;

    [self.window makeKeyAndVisible];
    return YES;
}
4

2 回答 2

1

当您创建您ViewController的内部时,AppDelegate.m您必须将其包围navigationController以使其工作

像这样的东西:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navigation;
    [navigation release];

    [self.window makeKeyAndVisible];
    return YES;
}
于 2013-03-09T21:14:17.093 回答
0

首先要检查的几件事:你的-buttonPressed方法真的被调用了吗?您是否正在获取并将非零值传递给-pushViewController:animated:?当您拨打电话时实际上是self.navigationController非零?

您的代码看起来很好。

于 2013-03-09T21:07:44.593 回答