无法导航到另一个ViewController

时间:2013-03-09 21:00:51

标签: iphone ios objective-c uinavigationcontroller uinavigationbar

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

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

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;
}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

您的代码看起来很好。