【问题标题】:can't navigate to another ViewController无法导航到另一个 ViewController
【发布时间】:2013-03-09 21:00:50
【问题描述】:

我是新手。 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;
}

【问题讨论】:

  • 是导航控制器中的第一个viewcontroller吗?
  • ViewController 是第一个 ViewController。 EnglishViewController是第二个ViewController
  • 你应该用导航控制器包围 ViewController 以使其工作。我会回答

标签: iphone ios objective-c uinavigationcontroller uinavigationbar


【解决方案1】:

当您在 AppDelegate.m 中创建您的 ViewController 时,您必须用 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;
}

【讨论】:

  • 连第一个视图都没有出现。我将 AppDelegate.m didFinishLaunchingWithOptions 函数添加到问题中。 UINavigationController 丢失。
【解决方案2】:

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

您的代码看起来不错。

【讨论】:

  • 是的,它正在调用。我在xib为buttonPressed做了连接。也许问题是我没有对委托做任何事情,但我认为我不需要它,因为不会互相发送数据。
猜你喜欢
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
相关资源
最近更新 更多