【问题标题】:How do I push to new view controller with code?如何使用代码推送到新的视图控制器?
【发布时间】:2013-02-10 01:06:46
【问题描述】:

应用程序应根据数据库中的某些数据转发到另一个视图控制器。数据库工作正常,但我无法让应用程序转发到第二个视图控制器。 这是我的代码。

ssSecondViewController *tempView =[[ssSecondViewController alloc] init];
[self.navigationController pushViewController:tempView animated:YES];

我没有收到任何错误,但是当我运行代码时,屏幕会转到一个空白的黑色视图?

【问题讨论】:

  • 我应该补充一点,我正在使用故事板和 xcode 4.5

标签: iphone ios uiviewcontroller xcode4.5


【解决方案1】:

如果您已经在情节提要上拥有 viewController,那么您可以通过如下代码分配它,并且您必须在情节提要上为 viewControler 设置标识符

ssSecondViewController *tempView =[self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

在这里设置你的viewController id,在下面的图像标识符是“MenuViewController”

如果它在 .Xib 文件上,那么

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithNibName:@"yourIdentifier" bundle:nil];

如果你只是想通过编程方式添加新的 viewController 而不使用已经可用的 viewController ,那么你必须用 initWithFrame 初始化它,然后它会显示 viewController

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithFrame:CGRectMake(0,0,320,480)];

ssSecondViewController *tempView =[[ssSecondViewController alloc] init];这一行只是分配了viewController,它没有任何位置可以绘制它。

【讨论】:

  • 非常感谢。你的第一个答案(故事板)是我需要的。像魅力一样工作。太棒了!
【解决方案2】:

你需要使用

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

因此,对于您的示例,它将是:

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithNibName:@"ssSecondViewController" bundle:nil];

【讨论】:

    【解决方案3】:

    对于通常不实际执行推送的情节提要,您可以在情节提要中使用 segueIdentifiers 设置进行转场:

    [self performSegueWithIdentifier:@"ssSecondSegue" sender:self];
    

    然后在您的prepareForSegue 中处理数据传递和其他设置器

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 2020-11-24
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多